Я новичок в XAML, поэтому простите меня за этот основной вопрос. В основном я пытаюсь изменить цвет выбранной строки в DataGrid. В настоящее время, когда выбрана строка, цвет по умолчанию светло-голубой, но я не понимаю, откуда он установлен. Я хочу изменить выбранный цвет, а также цвет текста выбранной строки. Ниже мой код.

        <DataGrid Name="TransferCallDataGrid"
              Margin="0 10 0 0"
              IsReadOnly="True"
              ItemsSource="{Binding Agents}" 
              SelectedItem="{Binding SelectedAgent}"
              AutoGenerateColumns="False" 
              HeadersVisibility="Column" 
              HorizontalAlignment="Stretch" 
              HorizontalGridLinesBrush="LightGray" 
              VerticalGridLinesBrush="Transparent" 
              HorizontalScrollBarVisibility="Disabled" 
              CanUserAddRows="False" 
              CanUserSortColumns="False"
              CanUserReorderColumns="False"
              BorderBrush="Transparent" 
              SelectionUnit="FullRow" 

              RowStyle="{DynamicResource DataGridRowStyle1}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="ID" Width=".7*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="2"/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <Rectangle Grid.Column="0" Fill="{Binding Path=AgentState, Converter={StaticResource AgentStateConverterResource}}" Width="3" Margin="0" HorizontalAlignment="Left"/>
                                    <TextBlock Grid.Column="1" TextAlignment="Center" Text="{Binding Id}" Style="{DynamicResource GridTextColumnStyle}"/>
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="First Name" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextAlignment="Center" Text="{Binding FirstName}" Style="{DynamicResource GridTextColumnStyle}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="Last Name" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextAlignment="Center" Text="{Binding LastName}" Style="{DynamicResource GridTextColumnStyle}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="State" Width="1.1*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextAlignment="Center" Text="{Binding Path=AgentState, Converter={StaticResource AgentStateTextConverterResource}}" Style="{DynamicResource GridTextColumnStyle}" Foreground="{Binding Path=AgentState, Converter={StaticResource AgentStateConverterResource}}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

И ниже приведен стиль строки, которую я считаю

  <Style x:Key="DataGridRowStyle1" TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        <Setter Property="ValidationErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border x:Name="DGR_Border" BorderBrush="Transparent" BorderThickness="0" Background="White" 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}"/>
                            <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                            <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>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsNewItem" Value="True">
                <Setter Property="Margin" Value="{Binding NewItemMargin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
            </Trigger>

        </Style.Triggers>

    </Style>

Любая помощь была бы очень признательна.

ТИА .

< Сильный > ------------------------- EDIT --------------------- ------------------------------

Это отредактированный блок кода. Теперь я могу изменить цвет фона, но не могу изменить цвет переднего плана. Мой код ниже.

<USD:DynamicsBaseHostedControl.Resources>
    <demo:AgentStateConverter x:Key="AgentStateConverterResource"/>
    <demo:AgentStateTextConverter x:Key="AgentStateTextConverterResource"/>

    <Style x:Key="GridTextColumnStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="FontWeight" Value="SemiBold"/>
        <Setter Property="Foreground" Value="#FF686767"/>
        <Setter Property="TextWrapping" Value="NoWrap"/>
        <Setter Property="TextTrimming" Value="None"/>
        <Setter Property="FontFamily" Value="Segoe UI Semibold"/>
    </Style>


    <Style x:Key="DataGridRowStyle1" TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>

        <Setter Property="ValidationErrorTemplate">

            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border x:Name="DGR_Border" BorderBrush="Transparent" BorderThickness="0" Background="White" 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}"/>
                            <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                            <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>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsNewItem" Value="True">
                <Setter Property="Margin" Value="{Binding NewItemMargin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
            </Trigger>

            <!--<Trigger Property="IsSelected" Value="True">
        <Setter Property="BorderBrush" Value="Blue" />
                <Setter Property="Background" Value="Black" />
                <Setter Property="Foreground" Value="Green"/>
            </Trigger>-->
        </Style.Triggers>
        <!--<Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue" />
        </Style.Resources>-->
    </Style>

    <Style TargetType="DataGridCell">


            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="Yellow" />
                    <Setter Property="Background" Value="#022648" />
                </Trigger>

            </Style.Triggers>

    </Style>
0
Faran Saleem 24 Сен 2018 в 10:23

2 ответа

Лучший ответ

Я размещаю здесь весь свой код, который работает с цветом переднего плана. Возможно, у вас есть какой-то другой стиль, мешающий переднему плану ячейки ... (Это очень быстрый и грязный пример, но цвет текста желтый, если он выбран)

<Window x:Class="WpfApp3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp3"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Grid.Resources>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="Yellow" />
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="DataGridRowStyle1" TargetType="{x:Type DataGridRow}">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
            <Setter Property="ValidationErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridRow}">
                        <Border x:Name="DGR_Border" BorderBrush="Transparent" BorderThickness="0" Background="White" 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}"/>
                                <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                            </SelectiveScrollingGrid>
                        </Border>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsNewItem" Value="True">
                    <Setter Property="Margin" Value="{Binding NewItemMargin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                </Trigger>
                <Trigger Property="IsSelected"
                         Value="True">
                    <Setter Property="BorderBrush"
                            Value="Red" />
                    <Setter Property="BorderThickness"
                            Value="2" />
                </Trigger>

            </Style.Triggers>

        </Style>
        <XmlDataProvider x:Key="Employees" XPath="/Employees/*">
            <x:XData>
                <Employees xmlns="">
                    <Employee Name="Terry Adams" Type="FTE" EmployeeNumber="1" />
                    <Employee Name="Claire O&apos;Donnell" Type="FTE" EmployeeNumber="12345" />
                    <Employee Name="Palle Peterson" Type="FTE" EmployeeNumber="5678" />
                    <Employee Name="Amy E. Alberts" Type="CSG" EmployeeNumber="99222" />
                    <Employee Name="Stefan Hesse" Type="Vendor" EmployeeNumber="-" />
                </Employees>
            </x:XData>
        </XmlDataProvider>
    </Grid.Resources>
    <DataGrid Name="TransferCallDataGrid"
          Margin="0 10 0 0"
          IsReadOnly="True"
          ItemsSource="{Binding Source={StaticResource Employees}}" 
          SelectedItem="{Binding SelectedAgent}"
          AutoGenerateColumns="False" 
          HeadersVisibility="Column" 
          HorizontalAlignment="Stretch" 
          HorizontalGridLinesBrush="LightGray" 
          VerticalGridLinesBrush="Transparent" 
          HorizontalScrollBarVisibility="Disabled" 
          CanUserAddRows="False" 
          CanUserSortColumns="False"
          CanUserReorderColumns="False"
          BorderBrush="Transparent" 
          SelectionUnit="FullRow" 

          RowStyle="{DynamicResource DataGridRowStyle1}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="ID" Width=".7*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="2"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Rectangle Grid.Column="0"  Width="3" Margin="0" HorizontalAlignment="Left"/>
                            <TextBlock Grid.Column="1" TextAlignment="Center" Text="{Binding Id}" />
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="First Name" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock TextAlignment="Center" Text="{Binding}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="Last Name" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock TextAlignment="Center" Text="{Binding}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="State" Width="1.1*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock TextAlignment="Center" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
0
Ketobomb 24 Сен 2018 в 08:58

Вы можете сделать это в стиле DataGridCell:

<Style TargetType="DataGridCell">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Yellow" />
                </Trigger>
            </Style.Triggers>
        </Style>

Чтобы ответить на ваш вопрос, откуда берется цвет по умолчанию: он берется из Systemcolors.HighlightBrushKey.

Вы можете переопределить это, и это было бы другим решением. Добавьте это в свои ресурсы:

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
               Color="#FF00aa"/>
0
Ketobomb 24 Сен 2018 в 08:06