如何将不同的样式应用于数据网格列
How to apply different styles to datagrid columns
我是 WPF 的新手,我不知道如何为数据网格的列(总是有 2 列)使用两个不同的控件模板。
这是 DataGrid 的 XAML:
<DataGrid x:Name="HomeSoftwareGrid"
CanUserAddRows="false"
ItemsSource="{Binding CollectedSoftwares}"
AutoGenerateColumns="True"
FontSize="15"
ColumnWidth="*"
IsReadOnly="True"
AutoGeneratingColumn="OnAutoGeneratingColumn"
CellEditEnding="OnCellEditEnding"
HorizontalAlignment="Center"
MaxWidth="600">
</DataGrid>
我正在使用 属性 AutoGeneratingColumn 删除特定列并编辑列的 headers
private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
PropertyDescriptor propertyDescriptor = (PropertyDescriptor)e.PropertyDescriptor;
e.Column.Header = propertyDescriptor.DisplayName;
if (propertyDescriptor.DisplayName == "Resources")
{
e.Cancel = true;
}
else if (propertyDescriptor.DisplayName == "SoftwareStatus")
{
e.Column.Header = "Software Status";
}
else if (propertyDescriptor.DisplayName == "SoftwareName")
{
e.Column.Header = "Software Name";
}
}
这些是我想使用的内容模板,第一个用于第一列,第二个显然用于第二列 :D :
<!-- first column style -->
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<ContentPresenter Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- second column style -->
<Style x:Key="SoftwareStatusDataGridColumn" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Rectangle x:Name="ImgPartially" Grid.Column="0" Width="20" Height="20" Fill="Yellow">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Uniform" Visual="{StaticResource appbar_warning}" />
</Rectangle.OpacityMask>
</Rectangle>
<ContentPresenter Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我怎样才能做到这一点?
sender
的事件是DataGrid,DataGrid可以在其可视化树中找到单元格样式。然后您可以将该样式单独分配给列:
else if (propertyDescriptor.DisplayName == "SoftwareName")
{
e.Column.Header = "Software Name";
e.Column.CellStyle = (sender as FrameworkElement).FindResource("SoftwareStatusDataGridColumn") as Style;
}
<Style TargetType="{x:Type DataGridCell}">
此Style使用Type作为键,默认分配给DataGridCells,无需在后面的代码中显式设置
我是 WPF 的新手,我不知道如何为数据网格的列(总是有 2 列)使用两个不同的控件模板。 这是 DataGrid 的 XAML:
<DataGrid x:Name="HomeSoftwareGrid"
CanUserAddRows="false"
ItemsSource="{Binding CollectedSoftwares}"
AutoGenerateColumns="True"
FontSize="15"
ColumnWidth="*"
IsReadOnly="True"
AutoGeneratingColumn="OnAutoGeneratingColumn"
CellEditEnding="OnCellEditEnding"
HorizontalAlignment="Center"
MaxWidth="600">
</DataGrid>
我正在使用 属性 AutoGeneratingColumn 删除特定列并编辑列的 headers
private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
PropertyDescriptor propertyDescriptor = (PropertyDescriptor)e.PropertyDescriptor;
e.Column.Header = propertyDescriptor.DisplayName;
if (propertyDescriptor.DisplayName == "Resources")
{
e.Cancel = true;
}
else if (propertyDescriptor.DisplayName == "SoftwareStatus")
{
e.Column.Header = "Software Status";
}
else if (propertyDescriptor.DisplayName == "SoftwareName")
{
e.Column.Header = "Software Name";
}
}
这些是我想使用的内容模板,第一个用于第一列,第二个显然用于第二列 :D :
<!-- first column style -->
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<ContentPresenter Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- second column style -->
<Style x:Key="SoftwareStatusDataGridColumn" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Rectangle x:Name="ImgPartially" Grid.Column="0" Width="20" Height="20" Fill="Yellow">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Uniform" Visual="{StaticResource appbar_warning}" />
</Rectangle.OpacityMask>
</Rectangle>
<ContentPresenter Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我怎样才能做到这一点?
sender
的事件是DataGrid,DataGrid可以在其可视化树中找到单元格样式。然后您可以将该样式单独分配给列:
else if (propertyDescriptor.DisplayName == "SoftwareName")
{
e.Column.Header = "Software Name";
e.Column.CellStyle = (sender as FrameworkElement).FindResource("SoftwareStatusDataGridColumn") as Style;
}
<Style TargetType="{x:Type DataGridCell}">
此Style使用Type作为键,默认分配给DataGridCells,无需在后面的代码中显式设置