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

时间:2011-08-08 10:00:47

标签: c# .net wpf xaml

我希望在基于WPf的桌面应用程序中的网格视图中有两个上下文菜单。 目前我能够显示一个上下文菜单,但我想在一个条件上显示上下文菜单1,在另一个条件下显示上下文菜单2。怎么做?

我按照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>

1 个答案:

答案 0 :(得分:3)

DataGrid上的触发器可以帮助您。以下代码仅供参考......

<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>

当然条件1和条件2必须互相排斥。如果它们都适用于数据网格,那么由于订单Condition2ContextMenu将优先。

如果这有帮助,请告诉我......