簡體   English   中英

有沒有辦法將 treeViewItem 名稱作為命令參數傳遞?

[英]Is there a way to pass treeViewItem name as command parameter?

我正在創建一個簡單的 treeview 並綁定到一個集合。 我想要的是在選擇 treeview 元素時在列表視圖中顯示有關集合元素的完整信息。 類似於 windows 文件瀏覽器。 問題是我無法將選定的 treeviewitem 綁定到命令。

嘗試使用 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 名稱作為命令參數而不是??? 在我的代碼中,因此選擇的 treeview 項目將按名稱標識,因此我可以獲得相應的信息並將其綁定到列表視圖。

使用RelativeSource解決了它。

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

工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM