gpt4 book ai didi

c# - 将默认资源样式应用于所有 DataGridTextColumns

转载 作者:行者123 更新时间:2023-11-30 20:11:04 26 4
gpt4 key购买 nike

我有几个数字列,希望它们右对齐。这是一个有点人为的例子来说明我的问题:

<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework" xmlns:Windows="clr-namespace:System.Windows;assembly=PresentationFramework" xmlns:Data="clr-namespace:System.Windows.Data;assembly=PresentationFramework" Title="MyApp"
Width="Auto" Height="Auto" SizeToContent="WidthAndHeight" Loaded="Window_Loaded">
<Controls:DataGrid Name="MyDataGrid" IsReadOnly="True"
xmlns="http://schemas.microsoft.com/wpf/2008/toolkit" ItemsSource="{Data:Binding}" AutoGenerateColumns="True">
<Controls:DataGrid.Columns>
<!-- This is a product name and is left justified by default -->
<Controls:DataGridTextColumn Header="ProductName" Binding="{Data:Binding Path=ProductName}" />
<!-- The rest of the columns are numeric and I would like for them to be right justified -->
<Controls:DataGridTextColumn Header="ProductId1" Binding="{Data:Binding Path=ProductId1}" >
<Controls:DataGridTextColumn.ElementStyle>
<Windows:Style TargetType="Controls:TextBlock">
<Windows:Setter Property="HorizontalAlignment" Value="Right"/>
</Windows:Style>
</Controls:DataGridTextColumn.ElementStyle>
</Controls:DataGridTextColumn>
<Controls:DataGridTextColumn Header="ProductId2" Binding="{Data:Binding Path=ProductId2}" >
<Controls:DataGridTextColumn.ElementStyle>
<Windows:Style TargetType="Controls:TextBlock">
<Windows:Setter Property="HorizontalAlignment" Value="Right"/>
</Windows:Style>
</Controls:DataGridTextColumn.ElementStyle>
</Controls:DataGridTextColumn>
<Controls:DataGridTextColumn Header="ProductId3" Binding="{Data:Binding Path=ProductId3}" >
<Controls:DataGridTextColumn.ElementStyle>
<Windows:Style TargetType="Controls:TextBlock">
<Windows:Setter Property="HorizontalAlignment" Value="Right"/>
</Windows:Style>
</Controls:DataGridTextColumn.ElementStyle>
</Controls:DataGridTextColumn>
<!-- More numeric columns follow... -->
</Controls:DataGrid.Columns>
</Controls:DataGrid>
</Window>

除了第一列之外的所有列都重复了正确的对齐方式,看起来是多余的。如果我可以将此网格中的 DataGridTextColumns 设置为右对齐,那么我只需要明确地左对齐第一列。我该怎么做,也许使用样式作为资源?有没有更好的办法?

最佳答案

DataGridTextColumn 不是从 FrameworkElement 派生的,因此您不能为其创建开箱即用的样式。

完成您想要的操作的最简单方法是为 DataGridCell 创建样式并从那里右对齐 TextBlock。然后为不应包含此内容的列设置 CellStyle={x:Null}(或您可能想要的任何其他样式)

<Controls:DataGrid ...>
<Controls:DataGrid.Resources>
<Windows.Style TargetType="Controls:DataGridCell">
<Windows.Setter Property="TextBlock.HorizontalAlignment" Value="Right"/>
</Windows.Style>
</Controls:DataGrid.Resources>
<Controls:DataGrid.Columns>
<!-- This is a product name and is left justified by default -->
<Controls:DataGridTextColumn Header="ProductName"
Binding="{Binding Path=ProductName}"
CellStyle="{x:Null}"/>

更新

但是,如果您确实想将 Style 应用于 DataGridTextColumn,则需要这样的东西。

首先我们需要一个可以“持有风格”的帮助类。在其中,我们添加了我们希望能够设置样式的所有属性(不在 FrameworkElement 中)。在本例中为 ElementStyle。

public class DataGridTextColumnStyleHelper : FrameworkElement
{
public DataGridTextColumnStyleHelper(){}
public static readonly DependencyProperty ElementStyleProperty =
DependencyProperty.Register(
"ElementStyle",
typeof(Style),
typeof(DataGridTextColumnStyleHelper));
public Style ElementStyle
{
get { return (Style)GetValue(ElementStyleProperty); }
set { SetValue(ElementStyleProperty, value); }
}
}

然后我们在xaml中添加样式

<Style x:Key="DataGridTextColumnStyle"
TargetType="local:DataGridTextColumnStyleHelper">
<Setter Property="ElementStyle">
<Setter.Value>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
</Setter.Value>
</Setter>
</Style>

为了能够在 DataGridTextColumn 上应用此样式,我们需要另一个具有 TextColumnStyle 属性的 Helper 类。在其中,我们使用反射和 SetValue 应用样式。

public class MyDataGridHelper : DependencyObject 
{
private static readonly DependencyProperty TextColumnStyleProperty = DependencyProperty.RegisterAttached(
"TextColumnStyle",
typeof(Style),
typeof(MyDataGridHelper),
new PropertyMetadata(MyPropertyChangedCallback));
public static void SetTextColumnStyle(DependencyObject element, string value)
{
element.SetValue(TextColumnStyleProperty, value);
}
public static Style GetTextColumnStyle(DependencyObject element)
{
return (Style)element.GetValue(TextColumnStyleProperty);
}
private static void MyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (DesignerProperties.GetIsInDesignMode(d) == true)
{
return;
}
DataGridTextColumn textColumn = (DataGridTextColumn)d;
Style textColumnStyle = e.NewValue as Style;
foreach (SetterBase setterBase in textColumnStyle.Setters)
{
if (setterBase is Setter)
{
Setter setter = setterBase as Setter;
if (setter.Value is BindingBase)
{
//Not done yet..
}
else
{
Type type = textColumn.GetType();
PropertyInfo propertyInfo = type.GetProperty(setter.Property.Name);
propertyInfo.SetValue(textColumn, setter.Value, null);
}
}
}
}
}

最后,我们可以像这样在 DataGridTextColumn 上使用样式

<DataGridTextColumn Header="ProductId1"  Binding="{Binding Path=ProductId1}"
local:MyDataGridHelper.TextColumnStyle="{StaticResource DataGridTextColumnStyle}">

关于c# - 将默认资源样式应用于所有 DataGridTextColumns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4249082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com