【问题标题】:Is there a way to pass treeViewItem name as command parameter?有没有办法将 treeViewItem 名称作为命令参数传递?
【发布时间】:2020-02-26 15:28:27
【问题描述】:

我正在创建一个绑定到集合的简单树视图。我想要的是在选择树视图元素时在列表视图中显示有关集合元素的完整信息。类似于 windows 文件资源管理器。问题是我无法将选定的树视图项绑定到命令。

尝试使用 treeviewitem 名称参数将命令绑定到 SelectedItemChanged 事件 - 无效。 用一个简单的事件进行了尝试 - 变得很麻烦并打破了 MVVM 模式。必须有一种优雅的方法来解决这个问题,因为我正在尝试做的事情在不同的应用程序中非常普遍。

<TreeView Grid.Row="1" Background="AliceBlue" ItemsSource="{Binding TopPanelNodes}" Name="TopTreeView" SourceUpdated="TopTreeView_SourceUpdated" >
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Nodes}">
            <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" IsEnabled="False">
                <TextBlock Text="{Binding Name}" >
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectedItemChanged">
            <i:InvokeCommandAction Command="{Binding UpdateInspectorCommand }" CommandParameter=???                                                    />
        </i:EventTrigger>
    </i:Interaction.Triggers>                        
</TreeView>

我想将 treeviewitem 名称作为命令参数而不是 ???在我的代码中,选定的树视图项将按名称标识,因此我可以获取相应的信息并将其绑定到列表视图。

【问题讨论】:

    标签: c# xaml treeview


    【解决方案1】:

    使用RelativeSource 解决了这个问题。

    CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}}"

    工作正常。

    【讨论】:

      猜你喜欢
      • 2017-07-10
      • 1970-01-01
      • 2021-12-20
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多