[英]WPF style cells and rows in DataGrid
I want replace styles rows and cells in DataGrid. 我想替换DataGrid中的样式行和单元格。 But, i can apply my custom style only one: rows or cells. 但是,我只能应用一种自定义样式:行或单元格。 Example on code below: 以下代码示例:
<DataGrid AutoGenerateColumns="false" ItemsSource="{ Binding FinalCalculatingData}" CanUserResizeRows="False" SelectionMode="Single"
CanUserAddRows="False">
<DataGrid.ItemContainerStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsMerged}" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<SelectiveScrollingGrid>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</SelectiveScrollingGrid.ColumnDefinitions>
<Border BorderBrush="Black" BorderThickness="0,0,1,1" Grid.Column="1">
<TextBlock HorizontalAlignment="Center">
<TextBlock.Inlines>
<Run Text="{Binding NameNull}"/>
</TextBlock.Inlines>
</TextBlock>
</Border>
<DataGridRowHeader SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
</SelectiveScrollingGrid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Style>
</DataGrid.ItemContainerStyle>
<!-- etc -->
</DataGrid>
How i can apply my style to rows and cells at the same time? 如何将我的样式同时应用于行和单元格?
UPD1: UPD1:
UPD2 UPD2
Here, I added the ControlTemplate code for the row using your edits. 在这里,我使用您的编辑为该行添加了ControlTemplate代码。 Selecting the line began to work, but without color. 选择线开始起作用,但是没有颜色。
code: 码:
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}" SnapsToDevicePixels="True" x:Name="SelectedMergedRow">
<SelectiveScrollingGrid>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</SelectiveScrollingGrid.ColumnDefinitions>
<SelectiveScrollingGrid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</SelectiveScrollingGrid.RowDefinitions>
<DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<DataGridCellsPresenter.Template>
<ControlTemplate TargetType="DataGridCellsPresenter">
<DataGridCell Height="20">
<TextBlock HorizontalAlignment="Center" Text="{Binding NameNull}"/>
<DataGridCell.Style>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="True">
<Setter Property="Background" Value="#CCDAFF"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</DataTrigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridCell.Style>
</DataGridCell>
</ControlTemplate>
and gif: 和gif:
You can't remove the cells from a DataGridRow
and expect it to behave as usual. 您不能从DataGridRow
删除单元格并期望它像往常一样运行。 You could try to define a custom Style for the DataGridCellsPresenter
but you still need to make sure that row is selected when you click on the custom row. 您可以尝试为DataGridCellsPresenter
定义自定义样式,但是当您单击自定义行时,仍然需要确保选择了该行。 Something like this: 像这样:
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<SelectiveScrollingGrid>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</SelectiveScrollingGrid.ColumnDefinitions>
<SelectiveScrollingGrid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</SelectiveScrollingGrid.RowDefinitions>
<DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<DataGridCellsPresenter.Template>
<ControlTemplate TargetType="DataGridCellsPresenter">
<DataGridCell Height="20">
<TextBlock HorizontalAlignment="Center" Text="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridCell}}" />
<DataGridCell.Style>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_MouseLeftButtonDown" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</DataTrigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridCell.Style>
</DataGridCell>
</ControlTemplate>
</DataGridCellsPresenter.Template>
</DataGridCellsPresenter>
<DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
</SelectiveScrollingGrid>
</Border>
</ControlTemplate>
private void DataGridCell_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = (DataGridCell)sender;
DataGrid dataGrid = FindParent<DataGrid>(cell);
dataGrid.SelectedItem = cell.DataContext;
}
private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindParent<T>(parent);
}
hi to use a CellStyle use the DataGrid.CellStyle
property on the datagrid. 嗨,要使用CellStyle,请使用datagrid上的DataGrid.CellStyle
属性。
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<!-- my style -->
</Style>
</DataGrid.CellStyle>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.