【问题标题】:MVVM Command binding for contextemenu of TreeView ElementTreeView 元素的上下文菜单的 MVVM 命令绑定
【发布时间】:2020-05-08 19:25:22
【问题描述】:

我需要将我的ViewModel 的命令 (RelayCommand) 与treeviewitemtreeviewitem 的 conetxtmenu 元素上的单击事件绑定

ViewModel 命令

private RelayCommand _myElementCommand;
public RelayCommand MyCommand
{
    get
    {
        return _myElementCommand?? (_myElementCommand= new RelayCommand(
           x =>
           {
               //LoadData();
               MessageBox.Show("Clicked!");
           }));
    }
}

查看

<TreeView x:Name="tvUBR" ItemsSource="{Binding UBRList, UpdateSourceTrigger=PropertyChanged}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center" Text="{ Binding Description }">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Setter Property="Foreground" Value="Black" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsMatch}"
                                                Value="True">
                                    <Setter Property="Foreground" Value="Green" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                    <TextBlock.ContextMenu>
                        <ContextMenu>
                            <!--<MenuItem Header="Details" Command="{Binding DropedElementCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"/>-->
                            <MenuItem Header="Details">
                                <i:Interaction.Triggers>
                                <!-- EventName ??? -->
                                    <i:EventTrigger EventName="Click"> 
                                        <i:InvokeCommandAction Command="{Binding Path=PlacementTarget.Tag.DataContext.MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </MenuItem>
                        </ContextMenu>
                    </TextBlock.ContextMenu>
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectedItemChanged">
            <i:InvokeCommandAction Command="{Binding SelectedUBRElementCommand}" 
                                            CommandParameter="{Binding SelectedItem, ElementName=tvUBR}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="b:TreeViewItemBehavior.IsBroughtIntoViewWhenSelected" Value="True" />
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

知道如何实现吗?

【问题讨论】:

    标签: wpf mvvm binding command commandbinding


    【解决方案1】:

    您可以将TextBlockTag 属性绑定到视图模型,然后使用ContextMenuPlacementTarget 属性绑定到命令:

    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
        <StackPanel Orientation="Horizontal">
            <TextBlock VerticalAlignment="Center" Text="{ Binding Description }">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Tag" Value="{Binding DataContext, RelativeSource={RelativeSource AncestorType=TreeView}}" />
                        <Setter Property="Foreground" Value="Black" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsMatch}" Value="True">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
                <TextBlock.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Details"
                                  Command="{Binding PlacementTarget.Tag.MyCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
                    </ContextMenu>
                </TextBlock.ContextMenu>
            </TextBlock>
        </StackPanel>
    </HierarchicalDataTemplate>
    

    【讨论】:

    • 完美,这正是我想要的。谢谢
    • 如何将树视图的“SelectedItem”作为参数传递?
    • @blfuentes:如果您还有其他问题,请提出新问题。
    猜你喜欢
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2016-06-04
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    相关资源
    最近更新 更多