我有一个数据网格控件,它已禁用单元格与完全启用的单元格(一些单元格有下拉列表,文本框,复选框)。问题是,禁用单元格的样式与启用的单元格完全相同。我只想更改所有已禁用单元格的样式,以便用户清楚它们无法更改数据。这是我的XAML代码:
<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>
这对我不起作用。我做错了什么?
感谢!!!
答案 0 :(得分:2)
您可以将触发器放在ControlTemplate中。然后你在触发器中引用的任何属性,在这种情况下&#34; IsEnabled&#34;或者&#34; IsSelected&#34;,它将指向它的任何TargetType的属性(在本例中为DataGridCell),假设它将具有该数据类型的这样的属性。否则绑定就会中断。
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="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>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>