WPF DataGrid-禁用某些列选择 [英] WPF DataGrid - Disable certain column selection
问题描述
我想禁用对Columns Field3&的选择Field4
(见图片)
I would like to disable selection for Columns Field3 & Field4 (see image)
Field1& 2值是由用户输入的,取决于它来计算Field3& 4。
Field1&2 values are entered by the user, depending on it Field3&4 are calculated.
我想要的-
- 使用'tab'在输入单元格之间移动时,所选内容应从第1行第2字段移动到第2行第1字段。
- 使用箭头移动时,字段3&不能选择字段4。
- 可以删除行(提及这一点,因为我不确定实现其他几点后的行为)。
这是我当前的XAML-
This is my XAML presently -
<Window x:Class="OrderCalculator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OrderCalculator"
mc:Ignorable="d"
Title="Order Estimate" Background="LightGray" Height="500" Width="800" Name="UI">
<Grid>
<DataGrid x:Name="ItemGrid" CanUserAddRows ="True" ItemsSource="{Binding ElementName=UI, Path=Items, Mode=TwoWay}" AutoGenerateColumns="False" HorizontalAlignment="Left"
Height="410" Margin="10,10,10,10" VerticalAlignment="Top" Width="420" HeadersVisibility="Column">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="DarkGray"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Field1" Binding="{Binding Path=field1, UpdateSourceTrigger=PropertyChanged}" Width="70"/>
<DataGridTextColumn Header="Field2" Binding="{Binding Path=field2, UpdateSourceTrigger=PropertyChanged}" Width="90"/>
<DataGridTextColumn Header="Field3" Binding="{Binding Path=field3, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" Width="110"/>
<DataGridTextColumn Header="Field4" Binding="{Binding Path=field4, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
更新
玩转之后,我能够确保不会通过鼠标单击来选择它。但是使用箭头键和选项卡仍然可以实现。到目前为止,我已经知道了-
在XAML中的DataGrid.Resources-
After playing around I have been able to make sure that it doesn't get selected with mouse click. But its still possible with arrow keys and tab. So far I have this - Added the following in XAML under DataGrid.Resources-
<Style x:Key="DisableSelection" TargetType="DataGridCell">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
然后对于要禁用选择的列-
And then for the columns that I wanted to disable selection -
<DataGridTextColumn Header="Field3" CellStyle="{StaticResource DisableSelection}" Binding="{Binding Path=field3, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" Width="120"/>
但是如上所述,这并不能完全解决问题。
But as mentioned this doesn't solve it completely.
推荐答案
解决了!
将样式修改为-
Solved it! Modified the Style to -
<Style x:Key="DisableSelection" TargetType="DataGridCell">
<Setter Property="Focusable" Value="False"/> //Disables all selection methods (including keyboard inputs)
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
将 Focusable属性设置为false可以解决问题。 3行代码的其余部分确保该单元格看起来无法选择=没有突出显示,也没有选择粗边框,同时确保该文本仍然显示。
Setting 'Focusable' property to false did the trick. The rest of the 3 lines of code ensure the cell looks un-selectable = no highlights nor selected thick borders while ensuring the text still appears.
将此样式添加到列中我想完全禁用选择-
Added this style to the column I wanted to disable selection entirely -
<DataGridTextColumn Header="Field1" CellStyle="{StaticResource DisableSelection}"...../>
这篇关于WPF DataGrid-禁用某些列选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!