使用5行来右对齐DataGridTextColumn使我的XAML膨胀。
我如何 将此“右对齐代码块”放入样式并从属性中调用它,例如类= “rightAlignDataGridTextColumn”。
<toolkit:DataGridTextColumn Header="Order Date" Width="Auto"
Binding="{Binding OrderDate, StringFormat='{}{0:dd.MM.yyyy}'}"
IsReadOnly="False">
<toolkit:DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Right" />
</Style>
</toolkit:DataGridTextColumn.ElementStyle>
</toolkit:DataGridTextColumn>
<toolkit:DataGridTextColumn Header="Required Date" Width="Auto"
Binding="{Binding RequiredDate, StringFormat='{}{0:dd.MM.yyyy}'}"
IsReadOnly="False">
<toolkit:DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Right" />
</Style>
</toolkit:DataGridTextColumn.ElementStyle>
</toolkit:DataGridTextColumn>
<toolkit:DataGridTextColumn Header="Shipped Date" Width="Auto"
Binding="{Binding ShippedDate, StringFormat='{}{0:dd.MM.yyyy}'}"
IsReadOnly="False">
<toolkit:DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Right" />
</Style>
</toolkit:DataGridTextColumn.ElementStyle>
</toolkit:DataGridTextColumn>
答案 0 :(得分:23)
在ResourceDictionary中声明样式,如下所示:
<Window.Resources>
<Style x:Key="RightAlignStyle" TargetType="{x:Type TextBlock}">
<Setter Property="TextAlignment" Value="Right" />
</Style>
</Window.Resources>
然后在每个DataGridTextColumn
元素上引用此样式:
<toolkit:DataGridTextColumn ElementStyle="{StaticResource RightAlignStyle}" ... >