WPF样式单元格和DataGrid中的行

时间:2019-02-25 10:49:07

标签: c# wpf xaml

我想替换DataGrid中的样式行和单元格。但是,我只能应用一种自定义样式:行或单元格。以下代码示例:

<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>

我如何同时将样式应用于行和单元格?

UPD1:

enter image description here

UPD2

在这里,我使用您的编辑为该行添加了ControlTemplate代码。选择线开始起作用,但是没有颜色。

代码:

<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>

和gif:

enter image description here

2 个答案:

答案 0 :(得分:1)

您无法从DataGridRow中删除单元格,并期望它像往常一样运行。您可以尝试为DataGridCellsPresenter定义自定义样式,但是当您单击自定义行时,仍然需要确保选择了该行。像这样:

<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);
}

答案 1 :(得分:0)

hi要使用CellStyle,请使用数据网格上的DataGrid.CellStyle属性。

<DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <!-- my style -->
    </Style>
</DataGrid.CellStyle>