извините, я не понимаю ... Я использую явный триггер стилей в сетке данных WPF. То, что я хочу, - это простая строка с синим цветом текста, если моя переменная DisplayCat = 0, и с красным цветом текста, если DisplayCat = 1.
Кроме того, строка должна быть отмечена красными полями, если она выбрана. В невыбранных несфокусированных строках все работает нормально. Однако при выборе HighlightBrushKey
, HighlightTextBrushKey
, InactiveSelectionHighlightBrushKey
и InactiveSelectionHighlightTextBrushKey
переопределяют мои настройки MultiDataTrigger. Есть идеи, как это предотвратить? Ждем ваших идей ...
App.xaml:
Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="DataGridFancy" TargetType="DataGrid">
<Setter Property="RowHeaderWidth" Value="0"/>
<Setter Property="SelectionMode" Value="Single"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="AutoGenerateColumns" Value="False"/>
<Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Background" Value="{x:Null}"></Setter>
<Setter Property="GridLinesVisibility" Value="All"/>
<Setter Property="Margin" Value="0,5,0,5"></Setter>
<Setter Property="BorderThickness" Value="1,1,1,1"></Setter>
<Setter Property="BorderBrush" Value="Transparent"></Setter>
</Style>
<Style x:Key="DataGridGColumnheaderFancy" TargetType="DataGridColumnHeader">
<Setter Property="FontSize" Value="12"></Setter>
<Setter Property="Background" Value="{x:Null}"></Setter>
<Setter Property="Foreground" Value="Black"></Setter>
<Setter Property="SeparatorVisibility" Value="Collapsed"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>
<Style x:Key="DataGridRowFancy" TargetType="DataGridRow">
<Setter Property="Margin" Value="5"></Setter>
<Setter Property="FontSize" Value="12"></Setter>
<Setter Property="Foreground" Value="LightGray"></Setter>
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="1" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="False"/>
<Condition Binding="{Binding DisplayCat}" Value="0"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="RoyalBlue"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="False"/>
<Condition Binding="{Binding DisplayCat}" Value="1"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="Crimson"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="False"/>
<Condition Binding="{Binding DisplayCat}" Value="2"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="DarkOrange"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="False"/>
<Condition Binding="{Binding DisplayCat}" Value="3"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="Gold"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="False"/>
<Condition Binding="{Binding DisplayCat}" Value="4"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="Chartreuse"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="False"/>
<Condition Binding="{Binding DisplayCat}" Value="5"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="Violet"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="True"/>
<Condition Binding="{Binding DisplayCat}" Value="0"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Crimson"/>
<Setter Property="Foreground" Value="RoyalBlue"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="True"/>
<Condition Binding="{Binding DisplayCat}" Value="1"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Crimson"/>
<Setter Property="Foreground" Value="Crimson"/>
<Setter Property="Background" Value="DimGray"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="True"/>
<Condition Binding="{Binding DisplayCat}" Value="2"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Crimson"/>
<Setter Property="Foreground" Value="DarkOrange"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="True"/>
<Condition Binding="{Binding DisplayCat}" Value="3"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Crimson"/>
<Setter Property="Foreground" Value="Gold"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="True"/>
<Condition Binding="{Binding DisplayCat}" Value="4"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Crimson"/>
<Setter Property="Foreground" Value="Chartreuse"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="True"/>
<Condition Binding="{Binding DisplayCat}" Value="5"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" Value="Crimson"/>
<Setter Property="Foreground" Value="Violet"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="DataGridCellFancy" TargetType="DataGridCell">
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Style>
</Application.Resources>
</Application>
MainWindow.xaml:
Window x:Class="Test.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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Background="#FF3C3C3C">
<Grid>
<DataGrid x:Name="NameGrid" ColumnHeaderStyle="{StaticResource DataGridGColumnheaderFancy}" Style="{StaticResource DataGridFancy}" RowStyle="{StaticResource DataGridRowFancy}" CellStyle="{StaticResource DataGridCellFancy}" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*"/>
<DataGridTextColumn Header="DisplayCat" Binding="{Binding DisplayCat}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Collections.Generic;
using System.Windows;
namespace LotManager_Mobile
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
List<Person> mylist = new List<Person>();
public MainWindow()
{
InitializeComponent();
string[] names = new string[] { "Peter", "Paul", "Monica", "Daisy", "Mike" };
int i = 0;
foreach (string mystring in names)
{
Person person = new Person();
person.Name = mystring;
person.DisplayCat = i += 1;
mylist.Add(person);
}
NameGrid.ItemsSource = mylist;
}
public class Person
{
public string Name
{ get; set; } = null;
public int? DisplayCat
{ get; set; } = null;
}
}
}