如何改变前景的颜色
How to change color of foreground
请帮助我。
我有带 ComboBoxColumn 的 DataGrid,我必须更改前景文本。
例如:
- 如果 ComboBoxColumn 有值 "IT" 前景必须是 "Red"
- 如果 ComboBoxColumn 有值 "R&D" 前景必须是 "Yellow"
- 如果 ComboBoxColumn 有值 "Finance" 前景必须是 "Black"
<Page
x:Class="DataGridComboBoxColumnColor.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MSControls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:local="using:DataGridComboBoxColumnColor"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
<ColumnDefinition Width="0.7*"></ColumnDefinition>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<MSControls:DataGrid Grid.Column="1"
ItemsSource="{x:Bind Persons}"
AutoGenerateColumns="False"
ScrollViewer.HorizontalScrollMode="Enabled"
RowHeight="60">
<MSControls:DataGrid.Columns>
<MSControls:DataGridTextColumn Header="First
Name" Width="*"
Binding="{Binding FirstName}"/>
<MSControls:DataGridTextColumn Header="Last
Name" Width="*"
Binding="{Binding LastName}"/>
<MSControls:DataGridTextColumn Header="Position"
Width="*"
Binding="{Binding Position}"/>
<MSControls:DataGridComboBoxColumn Header="Department"
Width="*"
Binding="{Binding DepartmentId}"
ItemsSource="{x:Bind Departments}"
DisplayMemberPath="DepartmentName"/>
</MSControls:DataGrid.Columns>
</MSControls:DataGrid>
</Grid>
</Page>
代码隐藏
namespace DataGridComboBoxColumnColor
{
public class Department
{
public int DepartmentId {
get;
set;
}
public string DepartmentName {
get;
set;
}
}
public class Person
{
public int PersonId {
get;
set;
}
public int DepartmentId {
get;
set;
}
public string FirstName {
get;
set;
}
public string LastName {
get;
set;
}
public string Position {
get;
set;
}
}
public sealed partial class MainPage : Page
{
public List<Department> Departments {
get;
set;
}
public List<Person> Persons {
get;
set;
}
public MainPage()
{
this.InitializeComponent();
Departments = new List<Department>
{
new Department {
DepartmentId = 1, DepartmentName = "R&D"
}
,
new Department {
DepartmentId = 2, DepartmentName = "Finance"
}
,
new Department {
DepartmentId = 3, DepartmentName = "IT"
}
};
Persons = new List<Person>
{
new Person
{
PersonId = 1, DepartmentId = 3, FirstName = "Ronald", LastName = "Rumple",
Position = "Network Administrator"
}
,
new Person
{
PersonId = 2, DepartmentId = 1, FirstName = "Brett", LastName = "Banner",
Position = "Software Developer"
}
,
new Person
{
PersonId = 3, DepartmentId = 2, FirstName = "Alice", LastName = "Anderson",
Position = "Accountant"
}
};
}
}
}
How to change color of foreground
根据您的要求,更好的方法是使用IValueConverter
重新调整匹配的前景。不幸的是,DataGridTextColumn
的前景属性不是DependencyProperty
,所以我们不能直接使用转换器。我们可以使用 DataGridTemplateColumn
制作自定义单元格并在 CellTemplate
中添加一个 TextBlock,然后将前景与源绑定,如下所示。
转换器
public class ColorValueConverter : IValueConverter
{
//IT" foreground must be "Red" - If ComboBoxColumn have value "R&D" foreground must be "Yellow"
public object Convert(object value, Type targetType, object parameter, string language)
{
var solorbrush = new SolidColorBrush();
switch (value.ToString())
{
case "IT":
solorbrush.Color = Colors.Red;
break;
case "R&D":
solorbrush.Color = Colors.Yellow;
break;
case "Finance":
solorbrush.Color = Colors.Black;
break;
default:
solorbrush.Color = Colors.LightBlue;
break;
}
return solorbrush;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
用法
<Page.Resources>
<local:ColorValueConverter x:Key="ColorCoverter"/>
</Page.Resources>
<controls:DataGridTemplateColumn Header="ID">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Id}" Foreground="{Binding Id, Converter={StaticResource ColorCoverter}}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
请帮助我。 我有带 ComboBoxColumn 的 DataGrid,我必须更改前景文本。
例如: - 如果 ComboBoxColumn 有值 "IT" 前景必须是 "Red" - 如果 ComboBoxColumn 有值 "R&D" 前景必须是 "Yellow" - 如果 ComboBoxColumn 有值 "Finance" 前景必须是 "Black"
<Page
x:Class="DataGridComboBoxColumnColor.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MSControls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:local="using:DataGridComboBoxColumnColor"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
<ColumnDefinition Width="0.7*"></ColumnDefinition>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<MSControls:DataGrid Grid.Column="1"
ItemsSource="{x:Bind Persons}"
AutoGenerateColumns="False"
ScrollViewer.HorizontalScrollMode="Enabled"
RowHeight="60">
<MSControls:DataGrid.Columns>
<MSControls:DataGridTextColumn Header="First
Name" Width="*"
Binding="{Binding FirstName}"/>
<MSControls:DataGridTextColumn Header="Last
Name" Width="*"
Binding="{Binding LastName}"/>
<MSControls:DataGridTextColumn Header="Position"
Width="*"
Binding="{Binding Position}"/>
<MSControls:DataGridComboBoxColumn Header="Department"
Width="*"
Binding="{Binding DepartmentId}"
ItemsSource="{x:Bind Departments}"
DisplayMemberPath="DepartmentName"/>
</MSControls:DataGrid.Columns>
</MSControls:DataGrid>
</Grid>
</Page>
代码隐藏
namespace DataGridComboBoxColumnColor
{
public class Department
{
public int DepartmentId {
get;
set;
}
public string DepartmentName {
get;
set;
}
}
public class Person
{
public int PersonId {
get;
set;
}
public int DepartmentId {
get;
set;
}
public string FirstName {
get;
set;
}
public string LastName {
get;
set;
}
public string Position {
get;
set;
}
}
public sealed partial class MainPage : Page
{
public List<Department> Departments {
get;
set;
}
public List<Person> Persons {
get;
set;
}
public MainPage()
{
this.InitializeComponent();
Departments = new List<Department>
{
new Department {
DepartmentId = 1, DepartmentName = "R&D"
}
,
new Department {
DepartmentId = 2, DepartmentName = "Finance"
}
,
new Department {
DepartmentId = 3, DepartmentName = "IT"
}
};
Persons = new List<Person>
{
new Person
{
PersonId = 1, DepartmentId = 3, FirstName = "Ronald", LastName = "Rumple",
Position = "Network Administrator"
}
,
new Person
{
PersonId = 2, DepartmentId = 1, FirstName = "Brett", LastName = "Banner",
Position = "Software Developer"
}
,
new Person
{
PersonId = 3, DepartmentId = 2, FirstName = "Alice", LastName = "Anderson",
Position = "Accountant"
}
};
}
}
}
How to change color of foreground
根据您的要求,更好的方法是使用IValueConverter
重新调整匹配的前景。不幸的是,DataGridTextColumn
的前景属性不是DependencyProperty
,所以我们不能直接使用转换器。我们可以使用 DataGridTemplateColumn
制作自定义单元格并在 CellTemplate
中添加一个 TextBlock,然后将前景与源绑定,如下所示。
转换器
public class ColorValueConverter : IValueConverter
{
//IT" foreground must be "Red" - If ComboBoxColumn have value "R&D" foreground must be "Yellow"
public object Convert(object value, Type targetType, object parameter, string language)
{
var solorbrush = new SolidColorBrush();
switch (value.ToString())
{
case "IT":
solorbrush.Color = Colors.Red;
break;
case "R&D":
solorbrush.Color = Colors.Yellow;
break;
case "Finance":
solorbrush.Color = Colors.Black;
break;
default:
solorbrush.Color = Colors.LightBlue;
break;
}
return solorbrush;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
用法
<Page.Resources>
<local:ColorValueConverter x:Key="ColorCoverter"/>
</Page.Resources>
<controls:DataGridTemplateColumn Header="ID">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Id}" Foreground="{Binding Id, Converter={StaticResource ColorCoverter}}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>