Неявный стиль WPF DataGrid CellStyle применяется вместо явного - PullRequest
1 голос
/ 29 апреля 2019

Определенный неявный стиль DataGridCell применяется вместо явного стиля, установленного через стиль DataGrid (свойство DataGrid.CellStyle).Зачем?Как XAML оценивает, использовать ли в этом случае неявный стиль поверх явного?

Я пытаюсь создать стиль DataGrid, который также установит стиль DataGridCells.

Попытка удаления неявногоСтиль DataGridCell и все работает как положено, явный стиль работает правильно.

Как заставить их работать вместе?

Также обеспечивает стиль DataGridCell как ресурс стиля DataGrid.Как?Почему?

Неявный стиль DataGridCell

<!-- Implicit DataGridCell style -->
<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="FontSize" Value="14" />
    <Setter Property="Background" Value="White" />
    <Setter Property="BorderBrush" Value="White" />
    <Style.Triggers>
       <Trigger Property="IsSelected" Value="True">
           <Setter Property="Background" Value="White" />
            <Setter Property="BorderBrush" Value="White" />
        </Trigger>
    </Style.Triggers>
</Style>

Стиль DataGridCell, который я хочу применить

<!-- Explicit DataGridCellStyle -->
<Style x:Key="DataGridCellStyle" TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Foreground" Value="#ffffff" />
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1"
                                         MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                        <GradientStop Color="#FF3b85c8" Offset="0" />
                        <GradientStop Color="#FF0668b7" Offset="1" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Trigger>

    </Style.Triggers>
</Style>

Стиль DataGrid

<!-- DataGrid style -->
<Style x:Key="BaseDataGrid" TargetType="{x:Type DataGrid}">
    <Setter Property="CellStyle" Value="{StaticResource DataGridCellStyle}" />

    <!-- Uncommenting this makes the solution works even with Implicit style -->
    <!--<Style.Resources>
        <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource DataGridCellStyle}"/>
    </Style.Resources>-->

</Style>

Простой экземпляр Datagrid

<!-- Data grid itself -->
<DataGrid Style="{StaticResource BaseDataGrid}"
          AutoGenerateColumns="True"
          ItemsSource="{Binding ModelClasses}">
</DataGrid>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...