在我的应用程序中,我使用“DataGrid”动态绑定值集。同样基于绑定的值,我需要更改绑定值的前景色。
这是我的XAML和C#代码
XAML:
<my:DataGridTemplateColumn Header="Priority">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Left" Name="txtPriority" Text="{Binding MessagePriority}" FontWeight="Bold">
<TextBlock.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{Binding ForegroundBrush}" />
</Style>
</TextBlock.Resources>
</TextBlock>
</my:DataGrid.Columns>
</my:DataGrid>
C#:
private SolidColorBrush _foregroundBrush;
public SolidColorBrush ForegroundBrush
{
get
{
return _foregroundBrush;
}
set
{
if (_foregroundBrush != value)
{
_foregroundBrush = value;
RaisePropertyChanged(() => _foregroundBrush);
}
}
}
var color = (Color)ColorConverter.ConvertFromString("#FF00FF");
var brush = new SolidColorBrush(color);
ForegroundBrush = brush;
答案 0 :(得分:3)
有几种方法可以做到这一点。这在很大程度上取决于您的要求,所以如果您更好地和更详细地解释它们会更好。但是,由于您没有,我不能在此示例中使用您的确切详细信息,您将不得不使其适应您的项目。第一种方法是简单地使用DataTrigger
...这种方法适用于最多8种不同的值/颜色对:
<DataGrid ItemsSource="{Binding RadioButtonData}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Label}">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding SomeValue}" Value="Something">
<Setter Property="TextElement.Foreground" Value="LightGreen" />
</DataTrigger>
<DataTrigger Binding="{Binding SomeValue}" Value="Another thing">
<Setter Property="TextElement.Foreground" Value="LightBlue" />
</DataTrigger>
<DataTrigger Binding="{Binding SomeValue}" Value="A different thing">
<Setter Property="TextElement.Foreground" Value="LightPink" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
您可以使用的另一种方法是将Converter
课程与Binding
一起使用。您可以在许多在线帖子中找到这样做的详细示例......以下是一些:
How to set Foreground of DataGrid Column with ValueConverter
Datagrid AutoGenerateColumns="True" forecolor IValueConverter
答案 1 :(得分:1)
Hai Everyone,
First of all i thanks to all for see my question and at that time send your answers. Now, i got the answer, i have to use the converter class for convert color to Solidcolorbursh like as below:
public class ValueConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || !(value is decimal))
return new SolidColorBrush(ForegroundBrush);
var dValue = System.Convert.ToDecimal(value);
if (dValue < 0)
return new SolidColorBrush(ForegroundBrush);
else
return new SolidColorBrush(ForegroundBrush);
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
首先,我们可以对类名称空间进行decalre,如:
xmlns:convert="clr-namespace:Helper"
<Grid.Resources>
<convert:ValueConverter x:Key="ValueConverter"></convert:ValueConverter>
</Grid.Resources>
<my:DataGridTemplateColumn Header="Priority">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Left" Name="txtPriority" Text="{Binding Priority}" FontWeight="DemiBold" Foreground="{Binding Priority, Converter={StaticResource ValueConverter}}">
</TextBlock>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>