ItemsControl.ItemTemplate bound to different Collection than ItemsControl
I have a bound treeview with an ItemsControl that dynamically creates the treeview items at runtime. However, to do this I have the ItemsControl ItemsSource bound to a different collection than the treeview is bound to. This works, but the issue comes with binding the textboxes in the ItemsContol, what should be displayed is actually a member of the SubOrganLocations
but I can't seem to get the binding to work correctly. No matter what I do WPF is looking to bind to a property of the ItemsControl ItemsSource ProjectOrganLocation.LesionTypes
instead of SubOrganLocations
. Below is a snippet of the tree's XAML
<TreeView ItemsSource="{Binding GlobalOrganTree}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubOrganLocations}">
<StackPanel Orientation="Horizontal">
<ItemsControl x:Name="ItemsControlGrid" ItemsSource="{Binding Path=ProjectOrganLocation.LesionTypes, Source={StaticResource Locator}}" >
<ItemsControl.ItemsPanel>
开发者_开发知识库 <ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding SubOrganLocations, Path=OrganLocation.Labels}"
Width="75"
TextAlignment="Center"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
How can I get the textboxes bound to a SubOrganLocations
property instead of a ProjectOrganLocation.LesionTypes
property?
A RelativeSource is common when you need to bind to a DataContext that is higher in the tree:
<TextBox Text="{Binding Path=DataContext.Labels, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" />
This binds to a TreeViewItems's DataContext (an Organ) and gets the Labels from it.
精彩评论