[英]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"
}
};
}
}
}
如何改變前景的顏色
根據您的要求,更好的方法是使用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.