【发布时间】:2014-04-02 11:29:33
【问题描述】:
我想知道是否有办法以编程方式获取 TreeViewItem 的treview 父级或 WPF 中的示例。 目标是拥有 TreeView 的 DataContext 以便执行来自 ViewModel 的命令。 任何帮助将不胜感激。
这里是 xaml 代码。 AddDisplayProperty 是树视图的数据上下文的一部分。 我的另一个问题是如何从 DataTemplate 的 TextBlock 中找到 TreeViewItem。
<TreeView ItemsSource="{Binding DisplayProperties, Mode=OneWay}" MinHeight="20" AllowDrop="True">
<i:Interaction.Behaviors>
<local:FrameworkElementCommandDropBehavior DropCommand="{Binding AddDisplayPropertyCommand}" DropType="{x:Type local:SearchProperty}"/>
</i:Interaction.Behaviors>
<TreeView.Resources>
<DataTemplate DataType="{x:Type local:SearchProperty}">
<TextBlock Margin="5.0" Text="{Binding Path=LongDescription, Mode=OneWay}" AllowDrop="True">
<i:Interaction.Behaviors>
<local:FrameworkElementCommandDropBehavior DropCommand="[Binding AddDisplayPropertyCommand}" DropType="{x:Type local:SearchProperty}" DropParameters="{Binding}"/>
</i:Interaction.Behaviors>
</TextBlock>
</DataTemplate>
</TreeView.Resources>
</TreeView>
【问题讨论】:
-
您可以在设置
Commands 时为您的Bindings 设置RelativeSource。您能否展示您的 XAML,以便我们了解您是如何创建树视图的? -
感谢您的回答。我已经添加了 XAML 代码。