简体   繁体   English

WPF:仅选择一个单元格时显示上下文菜单

[英]WPF: Show Context Menu when Only one cell is selected

What is wrong with this code? 此代码有什么问题?

The DataTrigger on SelectedCells.Count is not firing up. SelectedCells.Count上的DataTrigger没有启动。

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Exclude.Exclude,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="True">
                <Setter Property="Background" Value="#FFEBEBEB"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=SelectedCells.Count,ElementName=dgAssets }" Value="1">
                <Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
                <Setter Property="Background" Value="Green"/>
            </DataTrigger>
        </Style.Triggers>                          
    </Style>
</DataGrid.RowStyle>

Trying to show the context menu , only when just a single cell is selected. 尝试显示上下文菜单,仅当仅选择一个单元格时。

The SelectedCells property of a DataGrid returns an internal object called SelectedCellsCollection . DataGridSelectedCells属性返回一个称为SelectedCellsCollection的内部对象。 It extends another class called VirtualizedCellInfoCollection . 它扩展了另一个名为VirtualizedCellInfoCollection的类。 It does not implement neither INotifyCollectionChanged nor INotifyPropertyChanged interfaces. 它既不实现INotifyCollectionChanged也不实现INotifyPropertyChanged接口。 Moreover its Count property is not a dependecy property. 此外,其Count属性不是依赖属性。

So your binding is not able to notify to your data trigger when the number of selected cells increases or decreases. 因此,当选定单元格的数量增加或减少时,绑定将无法通知数据触发器。 For the data trigger its value is always 0. 对于数据触发器,其值始终为0。

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

相关问题 WPF中DiagramSurface上的Selected项的上下文菜单 - context menu for Selected item on a DiagramSurface in WPF 如果未选择任何内容,请勿显示上下文菜单 - Don't show context menu if nothing is selected 使用上下文菜单时如何更改数据网格 WPF C# 中选定行的字体颜色 - How to change font color of selected row in datagrid WPF C# when using context menu 为什么 WPF 用户控件的上下文菜单仅显示用户控件内的特定位置? - why WPF context menu of a user control only show up particular places within the user control? 在选定的行上隐藏特定的上下文菜单DataGrid WPF MVMVM - Hide specific context menu in datagrid upon selected row wpf mvvm WPF上下文菜单数据绑定图标仅在检查时出现 - WPF context menu data-bound icon only appears when inspected wpf在数据网格中的可编辑单元格上动态调出上下文菜单 - wpf bring up a context menu dynamically on an editable cell in a datagrid WPF DataGrid:单击鼠标左键显示动态上下文菜单 - WPF DataGrid: Show dynamic context menu on left click 使WPF窗口的行为类似于上下文菜单(正确显示/隐藏它) - Make a WPF Window behave like a Context Menu (show/hide it properly) 仅当用户单击TreeView中的根节点时,如何显示上下文菜单? - How can I show a context menu only when the user has clicked the root node in a TreeView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM