Ячейки и строки в стиле WPF в DataGrid - PullRequest
0 голосов
/ 25 февраля 2019

Я хочу заменить стили строк и ячеек в 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 голосов
/ 26 февраля 2019

Вы не можете удалить ячейки из 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 голосов
/ 25 февраля 2019

привет, чтобы использовать CellStyle, используйте свойство DataGrid.CellStyle в сетке данных.

<DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <!-- my style -->
    </Style>
</DataGrid.CellStyle>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...