|
You should ideally bind the visibility of the grid and control this via your view model.
Set the binding in your style, the datacontext should be the view model and you should be able to control the visibility via a property.
This[^] simple example should help you out.
|
|
|
|
|
Dear all
I'm fairly new to WPF and I think i'm trying to do something nonstandard, but maybe you can help me:
what i'm trying to do is the following:
in my code behind i have a collection (List) of a object of type Period (List<period> periods)
the object Period itself has various properties, of which some are Lists as well (Public List<string> FileReferences).
In my UI i have a datagrid, which I want to bind to these properties of one object Period in the list with Periods.
how can i dynamically bind my datagrid, two way?
thanks
|
|
|
|
|
If I get you correctly, you have an Object Period, which has a property that returns a List<FileReference> called FileReferences. Now you want to bind a DataGrid to the FileReferences property of a specific Period.
To accomplish this you would need a Property in your code behind that returns a Period object (call this SelectedPeriod). Then you can bind the DataGrid's ItemsSource property to SelectedPeriod.FileReferences.
I would just like to say, that if you have access to the models, then you should change your collections to return ObservableCollections rather than Lists. ObservableCollections provide notifications about any additions, deletions or other changes, which the bound controls can use to automatically update their display.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Dear experts,
Currently i'm trying to mimic the behavior of the ContentControl Control in a FlowDocument environment. I like the ContentControl because it allows to display content in a different template based on the content's type. E.g.:
<ContentControl Content={Binding}/> will use DataTemplates defined higer up in the XAML in a ResourceDictionary:
<DataTemplate DataType={x:Type local:Person}>...</DataType>
<DataTemplate DataType={x:Type local:Pet}>...</DataType>
I already found Create Flexible UIs With Flow Documents And Data Binding. As a start I altered the ItemsContent as follows:
public class TemplatedContent : Section
{
private static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(TemplatedContent), new PropertyMetadata(OnContentChanged));
private static readonly DependencyProperty TemplateProperty = DependencyProperty.Register("Template", typeof(DataTemplate), typeof(TemplatedContent), new PropertyMetadata(OnTemplateChanged));
public TemplatedContent()
{
Loaded += TemplatedContent_Loaded;
}
private void TemplatedContent_Loaded(object sender, RoutedEventArgs e)
{
GenerateContent(Template, Content);
}
public object Content
{
get { return GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
public DataTemplate Template
{
get { return (DataTemplate)GetValue(TemplateProperty); }
set { SetValue(TemplateProperty, value); }
}
private void GenerateContent(DataTemplate template, object content)
{
Blocks.Clear();
if (template != null && content != null)
{
FrameworkContentElement element = Helpers.LoadDataTemplate(template);
element.DataContext = content;
Blocks.Add(Helpers.ConvertToBlock(content, element));
}
}
private void GenerateContent()
{
GenerateContent(Template, Content);
}
private void OnContentChanged(object newValue)
{
if (IsLoaded)
GenerateContent(Template, newValue);
}
private void OnTemplateChanged(DataTemplate newValue)
{
if (IsLoaded)
GenerateContent(newValue, Content);
}
private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((TemplatedContent)d).OnContentChanged(e.NewValue);
}
private static void OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((TemplatedContent)d).OnTemplateChanged((DataTemplate)e.NewValue);
}
}
As you can see it's a verly basic peace of code and it works as expected when i use it in the following manner:
<FlowDocument>
<local:TemplatedContent Content={Binding}>
<local:TemplatedContent.Template>
<DataTemplate DataType={x:Type local:Person}>...</DataTemplate>
</local:TemplatedContent.Template>
</local:TemplatedContent>
</FlowDocument>
This is all well but to support multiple templates for different content types i will need to define the DataTemplates in a resource dictionary:
<FlowDocument>
<local:TemplatedContent Content={Binding}>
</local:TemplatedContent>
</FlowDocument>
and higer up in a resource dictionary:
<DataTemplate DataType={x:Type local:Person}>...</DataTemplate>
Now, the TemplatedContent is unable to find the DataTemplate. How is this possible? If I understand the theory around DependencyProperty correctly it should look in the xaml tree for <datatemplate> entries that match the type of the content right? It doesn't. When setting a breakpoint on line:
private static void OnTemplateChanged
it's never called.
I hope you experts can help me further with this!
Lots of thanks in advance!
edit: sorry for the layoutbugs, i hope it's fixed now
modified 8-Dec-11 14:13pm.
|
|
|
|
|
How to make muti-geometryModel3D to make different colors in different trangles ? i may wanna build many trangle but must with different color , but i don't know how to do it . if someone knows , please tell me how to do , thank you very much
|
|
|
|
|
Hey guys,
I am using the following codes to draw a line, how can I extend it to draw multi-lines ( i.e. 10 lines or 100 lines )
Line V_Line = new Line();
V_Line.Stroke = System.Windows.Media.Brushes.Gray;
V_Line.StrokeThickness = 4;
V_Line.X1 = 0;
V_Line.Y1 = 0;
V_Line.X2 = New_Figure_Canvas.Width;
V_Line.Y2 = 0;
New_Figure_Canvas.Children.Add(V_Line);
|
|
|
|
|
Put it in a loop. This will draw 10 lines at 10 pixel intervals on your canvas:-
for (int i = 0; i < 10; i++)
{
Line V_Line = new Line();
V_Line.Stroke = System.Windows.Media.Brushes.Gray;
V_Line.StrokeThickness = 4;
V_Line.X1 = 0;
V_Line.Y1 = i*10;
V_Line.X2 = canvas1.Width;
V_Line.Y2 = i*10;
canvas1.Children.Add(V_Line);
}
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
but if I increase the 10 to 100 then for 100 times I will make an instance of Line, is it optimal or there is another way ?!
|
|
|
|
|
That is how loops work. Don't be afraid to try.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Thanks
I tried it before, but I was wondering if there is any better ( optimal ) way of doing this
|
|
|
|
|
As you are going to be composing a large(?) number of graphic objects, I would suggest that you look to use a geometry instead. Basically geometries inherit from Freezable, which means that they can be frozen, which helps to increase performance. The item you are looking for is the LineGeometry class.
|
|
|
|
|
I have a collection of FolderModel and FileModel object. The FolderModel has 2 list properties holding FolderModels and FileModels. A folder can contain a list of Folders and/or a list of Files. It's populated like this:
RootFolder
|_Folder_A
|_ File_1
|_ File_2
|_ Folder_A1
|_ File_5
|_ File_6
|_ Folder_A2
|_Folder_B
|_ File_3
|_ File_4
|_ Folder_B1
I'm trying display te data in a treeview. My templates are:
<HierarchicalDataTemplate DataType="{x:Type models:FolderModel}"
ItemsSource="{Binding Path=Folders}">
<StackPanel Orientation="Horizontal"
Margin="2">
<TextBlock Text="{Binding Path=Caption}"/>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type models:FileModel}">
<StackPanel Orientation="Horizontal"
Margin="2">
<TextBlock Margin="0,0,5,0">
<Hyperlink NavigateUri="{Binding Caption}"
Foreground="#0C2DAA"
Command="{Binding SelectedLinkCommand}">
<InlineUIContainer>
<TextBlock Text="{Binding Caption}" />
</InlineUIContainer>
<Hyperlink.Style>
<Style TargetType="Hyperlink">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="TextDecorations" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
</Hyperlink.Style>
</Hyperlink>
</TextBlock>
</StackPanel>
</DataTemplate>
When I run it, all I see are the folders. The files are not showing up. What am I doing wrong?
Everything makes sense in someone's mind
|
|
|
|
|
The target objects need to be the same type. I use a UINode object that has all the properties needed to service a treeview node, Label, Tag and List<uinodes> being the minimum but I have order and image fields as well.
I then load the top nodes with the text I need to see and put the record (folder or file) object into the tag property (which is object).
Then for each top node I load the ChildNodes collection. This is a simple recursive excercise.
When I need to work with the content of the node I check the type of the object in the tag property first.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
They don't need to be the same type... heck, I can have a collection of objects and put random crap in there. You can only have one ItemsSource though. Thats his issue.
|
|
|
|
|
SledgeHammer01 wrote: You can only have one ItemsSource though. Thats his issue.
And how many types can you put into and ItemSource?
Stating you can only have 1 ItemSource does not solve the problem which is how to get multiple types into 1 ItemSource.
SledgeHammer01 wrote: I can have a collection of objects and put random crap in there
Isn't that what I just explained how to do?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yeah, I guess you kind of did . It wasn't very clear the first time I read it that thats what you were getting at. I explained in more detail in my original response.
|
|
|
|
|
Hmm I find clarity can be a major issue, I find myself repeating a description using slightly different wording just to see the light of understanding appear in their eyes.
If you used a collection of objects what property would you use to display in the treenode?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Well, OP didn't get your response either . Now you and I are actually on the same page, but just arguing about semantics haha. Yeah, I guess you can't really do it with a generic object collection... in my original response, I suggested the use of a common base class and putting the properties there... but you really have to structure the properties so you have them for both types.
|
|
|
|
|
SledgeHammer01 wrote: Well, OP didn't get your response either
I know- as I said clarity can be an issue with me.
I'll be extremely interested in Kevin's ultimate solution to see if it is something new!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I don't agree with that. I have other projects with collections of different types, and this type of data template works fine. I just can't see what's wrong here.
Everything makes sense in someone's mind
|
|
|
|
|
I'll be interested in your eventual solution.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
See my other response. You can have multiple object types, but you must combine them into a SINGLE collection. Right now you have 2 separate collections a FolderCollection and a FileCollection or whatever you call them. You need to have a generic ObjectCollection or whatever and use the template to decide how to render... or you can manually insert into the tree, but thats lame. Just throwing that out there as an option though .
|
|
|
|
|
I don't think you can do it the way you are trying to. HierachialDataTemplate only supports a single ItemsSource at any given level. What you want to do is define your models something like:
FileModelBase
FileModel
FolderModel
and put all the common code into the base class. Then, instead of having a Folders collection and a Files collection, you just have a single collection that holds both types. You can use triggers in your data template to change the rendering based on the type. Well, not on the type, but maybe a bool or enum in FileModelBase.
|
|
|
|
|
Well, you and Mycroft were right. My confusion came from another project I did where I had 2 collections on each object. However, I went back & found that in that project I wasn't displaying the second collection.
I implemented Mycroft's solution - create a single class with a Children collection.
Thanks to you both.
Everything makes sense in someone's mind
|
|
|
|
|
Superficially, you could think that they are the same thing (indeed, a tooltip behaves just like a popup with the TooltipService controlling the lifetime and display of the popup). They aren't the same thing though - basically, a popup allows you a lot more programattic control over where it appears, how it's triggered and how it's unloaded, etc.
If you just want to display a little bit of text (possibly with an image) against a control, use a tooltip. If you want something that can be triggered over the top of something else, and can do things such as handling interaction, use a popup.
|
|
|
|