简体   繁体   English

在WPF网格行中显示多个上下文菜单

[英]Showing multiple Context menu in WPF grid row

I wish to have two context menu in grid view in my WPf based desktop application . 我希望在基于WPf的桌面应用程序的网格视图中有两个上下文菜单。 Currently i am able to display one context menu, but I want to show context menu 1 on one condition and context menu 2 on another condition . 目前,我能够显示一个上下文菜单,但是我想在一种情况下显示上下文菜单1,而在另一种情况下显示上下文菜单2。 How to do that? 怎么做?

I am usign following XAML code to show grid and context menu 我很喜欢以下XAML代码以显示网格和上下文菜单

<dg:UCGrid x:Name="grdLetVariables" Grid.Row="2" GridTypeSource="LetGrid"
                                                 DataContext="{Binding}" >
                                <dg:UCGrid.Resources>
                                    <x:Array Type="{x:Type sys:Object}" x:Key="GridExtensions">
                                        <MenuItem Header="Delete" Click="ContextMenuDelete">
                                            <MenuItem.Icon>
                                                <Image Height="10" Source="../images/Delete.png"/>
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <Separator />
                                        <MenuItem Header="Move Up" Click="MoveUpLetGrdRow">
                                            <MenuItem.Icon>
                                                <Image Height="14" Source="../images/UpMove.png"/>
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <MenuItem Header="Move Down" Click="MoveDownLetGrdRow">
                                            <MenuItem.Icon>
                                                <Image Height="14" Source="../images/DownMove.png"/>
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <Separator />
                                        <MenuItem Header="Cancel" Click="CancelLetGrdRowEdit"/>
                                    </x:Array>
                                </dg:UCGrid.Resources>
                                <dg:UCGrid.ContextMenu>
                                    <ContextMenu>
                                        <ContextMenu.ItemsSource>
                                            <CompositeCollection>
                                                <CollectionContainer Collection="{StaticResource GridExtensions}" />
                                            </CompositeCollection>
                                        </ContextMenu.ItemsSource>
                                    </ContextMenu>
                                </dg:UCGrid.ContextMenu>
                            </dg:UCGrid>
                        </Grid>

Triggers on DataGrid can help you here. DataGrid上的触发器可以在这里为您提供帮助。 Code below is just for illustration ... 以下代码仅用于说明...

<UserContorl.Resources>
    <ContextMenu x:Key="Condition1ContextMenu" ../>
    <ContextMenu x:Key="Condition2ContextMenu" ../>
</UserControl.Resources>
...
<Style TargetType="{x:Type dg:UCGrid}">
   <Style.Triggers>
      <DataTrigger Binding="{Binding Condition1}" Value="Value1">
          <Setter Property="ContextMenu" Value="{StaticResource Condition1ContextMenu}"/>
      </DataTrigger>
      <DataTrigger Binding="{Binding Condition2}" Value="Value2">
          <Setter Property="ContextMenu" Value="{StaticResource Condition2ContextMenu}"/>
      </DataTrigger>
   </Style.Triggers>
</Style>

Ofcourse condition1 and condition2 must be exclusive of each other. 当然condition1和condition2必须互斥。 If both of them are applicable on the data grid then due to order Condition2ContextMenu will take the precedence. 如果它们两者都适用于数据网格,则由于Condition2ContextMenu将优先。

Let me know if this helps... 让我知道这是否有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM