在使用鼠标右键单击时,Datagrid WPF被禁用的行被选中 [英] Disabled row on Datagrid WPF getting selected when clicked with the Right Mouse Button

查看:200
本文介绍了在使用鼠标右键单击时,Datagrid WPF被禁用的行被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我有一个datagrid,我想要将用户分配一个值的单行的IsEnabled属性设置为false到datagrid的绑定项itemSource



所以我通过datagrid做了样式触发器:

 < DataGrid AutoGenerateColumns =FalseMargin =9,35,0,6Name =dataGrid2ItemsSource ={Binding}SelectionChanged =dataGrid2_SelectionChangedIsReadOnly =TrueSelectionMode =单个> 
< DataGrid.RowStyle>


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



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

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



问题是:我仍然可以使用鼠标右键单击并选择禁用的行...IsEnabled属性只阻止鼠标左键点击datagrid行?我需要设置另一个属性来禁用鼠标右键单击该行吗?



谢谢!

解决方案

这是一个已知的 DataGrid 的错误,它的连接在这里报告: DatagridRow被选中右键单击即使禁用datagrid



要解决这个问题,您可以绑定 IsHitTestVisible to IsEnabled

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


I got some problems in Datagrid WPF

I have a datagrid, and I want to set the IsEnabled property of a single row to false whenever the user assign a value to the binding item of the datagrid itemSource

So I made it by datagrid Style triggers:

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

Working fine, it disables the entire row when a value is assigned to the "Coluna" field of that row (different of 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?

Thank you!

解决方案

This is a known bug of the DataGrid and it's reported on Connect here: DatagridRow gets selected on right click even if the datagrid is disabled. Looks like this will be fixed in WPF 4.5.

To workaround it you can bind IsHitTestVisible to 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>

这篇关于在使用鼠标右键单击时,Datagrid WPF被禁用的行被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆