Reputation: 4198
Hi I have a question and probably it is fairly simple, I have a data grid with styled cells and data triggers that xaml looks like:
<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="ToolTip" Value="Click to zoom it to hour view for selected day" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}" Value="X ">
<Setter Property="Background" Value="#FF6666" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Center" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}" Value="X">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Center" />
</DataTrigger>
</Style.Triggers>
</Style>
Well fairly simple, it does what it should when value is X it centres text and gives it proper background. But this background is a problem.
I want background fit the cell width, as now cell content text background is coloured as text width, height plus margins but does not fill whole cell. Can it be done some easy way??
I tried adding:
<Setter Property="Width" Value="{Binding ActualWidth, ElementName=parentElementName}" />
to data trigger but no luck.
Upvotes: 0
Views: 1125
Reputation: 35681
I don't think setting VerticalAlignment/HorizontalAlignment for DataGridCell is a good idea, since it produces some weird effects. VerticalContentAlignment/HorizontalContentAlignment sounds better but unfortunately DataGridCell ignores them.
DataGridCell content can be centered via ElementStyle of each column where element style (in this case TextBlock) changes alignment, e.g:
<DataGrid RowHeight="40">
<DataGrid.Resources>
<Style x:Key="txt" TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGrid.Resources>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="#FF6666" />
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn ElementStyle="{StaticResource txt}"
Binding="{Binding Path=Name}" Header="Name" />
</DataGrid.Columns>
</DataGrid>
Upvotes: 1