WPF编程创建树状的ItemTemplate /列 [英] WPF Programmatically create treeview itemtemplate/columns
问题描述
我有读取数据库表,并把它变成一个树状的应用程序。为树视图当前的ItemTemplate看起来是这样的:
<预类=郎咸平的XML prettyprint-覆盖>
< ; TreeView.ItemTemplate>
< HierarchicalDataTemplate的ItemsSource ={结合SubOrganLocations}>
<网格和GT;
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =*/>
< ColumnDefinition WIDTH =35/>
< ColumnDefinition WIDTH =35/>
< ColumnDefinition WIDTH =35/>
< /Grid.ColumnDefinitions>
< TextBlock的Grid.Column =0文本={结合OrganDisplayName}/>
<文本框Grid.Column =1IsEnabled =真/>
<文本框Grid.Column =2IsEnabled =真/>
<文本框Grid.Column =3IsEnabled =真/>
< /网格和GT;
< / HierarchicalDataTemplate>
< /TreeView.ItemTemplate>
但是,在将来有可能是需要添加更多的列(由数确定在表中不同的值),所以我试图动态地创建它。 ?我将如何去这样做。
这样的事情是可能的:
< TreeView.ItemTemplate>
< HierarchicalDataTemplate的ItemsSource ={结合SubOrganLocations}>
< StackPanel的方向=横向>
< TextBlock的文本={结合OrganDisplayName}/> !
< - 如果字段绑定到可通过收集暴露:
<的ItemsControl的ItemsSource ={绑定字段}> - >
<的ItemsControl的ItemsSource ={结合,转换器= {StaticResource的SomeCleverConverter}}>
< ItemsControl.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel的方向=横向/>
< / ItemsPanelTemplate>
< /ItemsControl.ItemsPanel>
< ItemsControl.ItemTemplate>
<&DataTemplate的GT;
<文本框的文本={绑定值}WIDTH =35/>
< / DataTemplate中>
< /ItemsControl.ItemTemplate>
< / ItemsControl的>
< / StackPanel的>
< / HierarchicalDataTemplate>
< /TreeView.ItemTemplate>
这取决于如果树型视图的DataContext的(该SubOrganLocation)可以公开字段的集合,或至少使用具有转换器来导出他们。最简单,最简单的可能会暴露领域的集合,所以你可以做 {绑定字段}
。
I have an application that reads database tables and puts it into a treeview. The current ItemTemplate for the treeview looks like this:
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubOrganLocations}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="35" />
<ColumnDefinition Width="35" />
<ColumnDefinition Width="35" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding OrganDisplayName}" />
<TextBox Grid.Column="1" IsEnabled="True" />
<TextBox Grid.Column="2" IsEnabled="True" />
<TextBox Grid.Column="3" IsEnabled="True" />
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
However, in the future there could be more columns that need to be added (determined by the number of distinct values in a table), so I'm trying to create it dynamically. How would I go about doing that?
Something like this might be possible:
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubOrganLocations}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding OrganDisplayName}" />
<!-- If the fields to bind to can be exposed via a collection:
<ItemsControl ItemsSource="{Binding Fields}"> -->
<ItemsControl ItemsSource="{Binding, Converter={StaticResource SomeCleverConverter}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Value}" Width="35" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
It depends if the DataContext of the TreeViewItem (the SubOrganLocation) can expose a collection of fields, or at least be used to derive them with a converter. The simplest and probably easiest would be to expose a collection of fields so you can just do {Binding Fields}
.
这篇关于WPF编程创建树状的ItemTemplate /列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!