【问题标题】:WPF Disabled DatagridRow and ContextMenuWPF 禁用 DatagridRow 和 ContextMenu
【发布时间】:2020-07-04 02:33:52
【问题描述】:

我有一个绑定到自定义对象集合的数据网格。

此数据网格允许用户在右键单击一行时访问上下文菜单。我通过 TextBlock 样式来做到这一点:

<Style x:Key="DatagridTextblockStyle"
    TargetType="{x:Type TextBlock}">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="First action" />
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>

如果自定义对象的“IsActive”布尔属性为 false,它还会显示可能被禁用的行。

我通过 DataGrid.RowStyle 做到这一点:

<DataGrid ItemsSource="{Binding MyCustomObjects}">
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsActive}"
                             Value="True">
                    <Setter Property="IsEnabled"
                            Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

这很好用。 然而问题是当一行被禁用时,上下文菜单不再可用。

我找不到解决办法。

有什么想法吗?

【问题讨论】:

  • 嘿@mm8,感谢您抽出时间来写评论并帮助我。这实际上起到了作用,我感觉很糟糕,我没有考虑过-_-。谢谢大佬。

标签: wpf datagrid contextmenu


【解决方案1】:

ElementStyle中将ContextMenuService.ShowOnDisabled附加属性设置为true

<Style x:Key="DatagridTextblockStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="ContextMenuService.ShowOnDisabled" Value="True" />
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="First action" />
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>

【讨论】:

  • 嘿@mm8,感谢您抽出时间来写评论并帮助我。这实际上起到了作用,我感觉很糟糕,我没有考虑它-_-。谢谢大佬。
  • @Louitbol:这里没什么好难过的。请继续提问!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
相关资源
最近更新 更多