我有一个数据网格控件已禁用与完全启用的单元格(某些单元格有下拉列表,文本框,复选框)组合的单元格。问题是禁用单元格的样式与启用的单元格完全相同。我只是想改变所有被禁用的单元格的样式,所以用户很清楚他们不能改变数据。这是我的XAML代码:在DataGrid中更改禁用单元格的背景颜色WPF
<DataGrid Name="DataGrid"
ItemsSource="{Binding MySource}"
AutoGenerateColumns="False" Grid.Row="1"
BorderThickness="0"
SelectionMode="Single" SelectionUnit="FullRow"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserReorderColumns="False" CanUserSortColumns="False"
CanUserResizeColumns="False" CanUserResizeRows="False"
BeginningEdit="DataGrid_BeginningEdit" Margin="10">
<DataGrid.Resources>
<CollectionViewSource Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.SourceList}" x:Key="SourceChoices" />
<CollectionViewSource Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MyDropDownSource}" x:Key="MyDropDownOptions" />
<CollectionViewSource Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MySource}" x:Key="MySourceOptions" />
<Style TargetType="DataGrid">
<Setter Property="GridLinesVisibility" Value="All" />
<Setter Property="HorizontalGridLinesBrush" Value="Gray"/>
<Setter Property="VerticalGridLinesBrush" Value="LightGray"/>
<Setter Property="FontSize" Value="13" />
</Style>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="LightGray" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontSize" Value="13" />
<Setter Property="FontWeight" Value="DemiBold" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Height" Value="34" />
</Style>
<Style TargetType="DataGridCell">
<Setter Property="Height" Value="35" />
<Setter Property="Padding" Value="4" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"/>
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Pink" />
<Setter Property="Foreground" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="White" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Width" Value="Auto" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Pos" Binding="{Binding Position}" Width="40" CanUserSort="False" />
<DataGridTextColumn Header="Acn Nbr" Binding="{Binding MySourceNumber1}" Width="10*" CanUserSort="False" />
<DataGridTextColumn Header="Name" Binding="{Binding MySourceNumber2}" Width="15*" CanUserSort="False" />
<DataGridTextColumn Header="Org #" Binding="{Binding MySourceNumber3}" Width="40" CanUserSort="False" />
<DataGridCheckBoxColumn Header="Proteus" Binding="{Binding MySourceNumber4}" Width="50" CanUserSort="False" />
<DataGridComboBoxColumn Header="Source Id" TextBinding="{Binding MySourceNumber5}" Width="10*" CanUserSort="False"
DisplayMemberPath="Name" SelectedValuePath="ID" ItemsSource="{Binding Source={StaticResource Options}}"/>
<DataGridComboBoxColumn Header="Bench" SelectedValueBinding="{Binding ID}" Width="10*" CanUserSort="False"
DisplayMemberPath="Name" SelectedValuePath="ID" ItemsSource="{Binding Source={StaticResource Options}}"/>
<DataGridComboBoxColumn Header="Org Id" SelectedValueBinding="{Binding ID}" Width="10*" CanUserSort="False"
DisplayMemberPath="OrganismAbbrev" SelectedValuePath="ID" ItemsSource="{Binding Source={StaticResource Options}}"/>
<DataGridTextColumn Header="Comment" Binding="{Binding Comment}" Width="20*" CanUserSort="False" />
</DataGrid.Columns>
</DataGrid>
注意,在代码中的一部分,上面写着:
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
这不是为我工作。我究竟做错了什么?
谢谢!
我将触发器移动到DataGridRow风格,没有骰子。 –
对不起,我正在看看“这不适合我”的部分,但是后来我又读了整个问题。所以,不起作用的是被禁用的单元应该有粉红色背景,对吧? –
我只是设置了另一个触发器来禁用某些单元格,并且它们变成了粉色背景。你如何禁用单元格? –