DataGrid中的WPF样式单元格和行

问题描述 投票:0回答:2

我想在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

c# wpf xaml
2个回答
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);
}

0
投票

嗨,要使用CellStyle,请在datagrid上使用DataGrid.CellStyle属性。

<DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <!-- my style -->
    </Style>
</DataGrid.CellStyle>
© www.soinside.com 2019 - 2024. All rights reserved.