首页 文章

使用鼠标右键单击时,Datagrid WPF上禁用的行被选中

提问于
浏览
3

我在Datagrid WPF中遇到了一些问题

我有一个datagrid,每当用户为datagrid itemSource的绑定项赋值时,我想将单行的IsEnabled属性设置为false

所以我通过datagrid Style触发器创建它:

<DataGrid AutoGenerateColumns="False" Margin="9,35,0,6" Name="dataGrid2" ItemsSource="{Binding}" SelectionChanged="dataGrid2_SelectionChanged" IsReadOnly="True" SelectionMode="Single">
                    <DataGrid.RowStyle>


                        <Style TargetType="{x:Type DataGridRow}">
                            <Style.Setters>
                                <Setter Property="IsEnabled" Value="False" />
                            </Style.Setters>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Coluna}" Value="{x:Null}">
                                    <Setter Property="IsEnabled" Value="True"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>



                    </DataGrid.RowStyle>
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Campo" Binding="{Binding Path=Campo}" Width="1.4*" CanUserSort="False" />
                        <DataGridTextColumn Header="Coluna/Constante" Binding="{Binding Path=Coluna}" CanUserSort="False" Width="*" />
                    </DataGrid.Columns>
                </DataGrid>

工作正常,当一个值分配给该行的“Coluna”字段时(不同的null),它会禁用整行

The problem is: I still can click and select the disabled row using the right mouse button... Does the "IsEnabled" property only blocks the left mouse button click on datagrid rows?? Do I need to set another property to disable the right mouse button click on that row?

谢谢!

1 回答

  • 5

    这是 DataGrid 的一个已知错误,它在Connect上报告:DatagridRow gets selected on right click even if the datagrid is disabled . 看起来这将在WPF 4.5中修复 .

    要解决此问题,您可以将 IsHitTestVisible 绑定到 IsEnabled

    <DataGrid ...>
        <DataGrid.RowStyle>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="IsEnabled" Value="False" />
                <Setter Property="IsHitTestVisible"
                        Value="{Binding RelativeSource={RelativeSource Self},
                                        Path=IsEnabled}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Coluna}" Value="{x:Null}">
                        <Setter Property="IsEnabled" Value="True"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
        <!-- ... -->
    </DataGrid>
    

相关问题