您好我有以下UserControl。 可以注意到,在第二个 HierarchicalDataTemplate 中也包含一个ListBox。
我希望ListBox ItemsSource绑定到viewModel中的一个字段,它是 CurrentPropertyValues 。
当我尝试在主窗口或树外部绑定它时,它可以工作,(只是ItemsSource =" {Binding CurrentPropertyValues}")
我尝试了各种各样的东西让它在HierarchicalDataTemplate中工作(它有不同的数据上下文),但我一直在失败。
感谢您的帮助!
<UserControl x:Class="RidaDiff2.Views.LeftTreeControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:converters="clr-namespace:RidaDiff2.Converters"
xmlns:model="clr-namespace:RidaDiff2.Model"
xmlns:viewModel="clr-namespace:RidaDiff2.ViewModels"
xmlns:views="clr-namespace:RidaDiff2.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<converters:EnumToPicConverter x:Key="Converter" />
<HierarchicalDataTemplate DataType="{x:Type model:TreeNode}" ItemsSource="{Binding ChildListNodes}" x:Key="NotSelected">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=EntityType,Converter={StaticResource Converter}}" />
<TextBlock Margin="5,0" Text="{Binding Name1}" />
<!--"{Binding ItemName, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" -->
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type model:TreeNode}" ItemsSource="{Binding ChildListNodes}" x:Key="Selected">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=EntityType,Converter={StaticResource Converter}}" />
<TextBlock Margin="5,0" Text="{Binding Name1}" />
</StackPanel>
<ListBox DataContext="viewModel:TreeViewModel" Height="300" Width="100" ItemsSource="{Binding Path=(viewModel:TreeViewModel.CurrentPropertyValues), Mode=TwoWay}" ></ListBox>
</StackPanel>
</HierarchicalDataTemplate>
</UserControl.Resources>
<views:ExtendedTreeView x:Name="Tree" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding RootNode}" SelectedItem_="{Binding CurrentCouple,Mode=TwoWay}">
<views:ExtendedTreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding Path=(model:TreeNode.IsExpanded), Mode=TwoWay}" />
<Setter Property="Background" Value="{Binding Path=(model:TreeNode.BackgroundColor), Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding Path=(model:TreeNode.IsSelected), Mode=TwoWay}"></Setter>
<Setter Property="HeaderTemplate" Value="{StaticResource NotSelected}"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="HeaderTemplate" Value="{StaticResource Selected}"/>
</Trigger>
</Style.Triggers>
</Style>
</views:ExtendedTreeView.ItemContainerStyle>
</views:ExtendedTreeView>
答案 0 :(得分:0)
如果CurrentPropertyValues
是TreeViewModel
的属性,那么您可以使用RelativeSource
绑定并向上查看可视树以查找TreeView
并将ItemsSource
绑定到属性它的DataContext
:
<ListBox
Height="300"
Width="100"
ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}, Path=DataContext.CurrentPropertyValues}" />