I'm just now entering the realm of WPF, and I would like to create a custom control. I'm not too concerned about the styling of it, but rather the functionality.
I'm trying to create a custom Tree View that auto-magically fills up with data from a different library so that other WPF applications are able to use this control, and expose this data to their users, and get feedback with ease.
IE:
+-----------------------------+
|+Project |
|+-- File |
|+---- Patch |
|+Other Project |
|+-- Files Are Nifty |
|+---- Yup. |
+-----------------------------+
I want ^that^ to be a re-useable control that should always have the same data among all of its instances. Essentially, a default data-binding. I've done a bit of googling, and I searched in here but all of the questions / answers either weren't relevant, were over my head, or both. The only bit of useful information I found was that in the data-provider to have two branches, return a CompositeCollection.
If somebody could explain this, step-by-step for a WPF Custom Control library, I would much appreciate it.
If your data is always in the same pattern (eg Projects have Files, which have Patches), you dont necessarily need a CompositeCollection. You could also use HierarchicalDataTemplates to display your data like so:
<HierarchicalDataTemplate x:Key="ProjectTemplate"
ItemsSource="{Binding ChildCollection}"
ItemTemplate="{StaticResource FileTemplate}">
<here comes the actual TreeViewItem of this Template>
</HierarchicalDataTemplate>
Your FileTemplate is another HierarchicalDataTemplate, this goes on until you reached the lowest level, this will be a DataTemplate. All these templates are placed in the resources, now all you have to to is setting the TreeView's ItemTemplate to the top level HierarchicalDataTemplate (ProjectTemplate):
<TreeView ItemsSource="{Binding Path=ChildCollection, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{StaticResource ResourceKey=ProjectTemplate}">
</TreeView>
Your Data could look like the following:
public class Project
{
public ObservableCollection<File> ChildCollection {get;set;}
}
A File has another ChildCollection (eg ObservableCollection<Patch>
) aso
To set a default DataContext, you could set the DataContext in the constructor to a ViewModel which has a ChildCollection of Type ObservableCollection<Project>
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.