使用IValueConverter的Silverlight Datagrid格式 [英] Silverlight Datagrid formatting using IValueConverter

查看:48
本文介绍了使用IValueConverter的Silverlight Datagrid格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现IValueConverter的转换器类.

公共类ColumnValueSelector:IValueConverter
{
IValueConverter.Convert对象(对象值,类型targetType,对象参数,CultureInfo文化)
{
StringRow row =(StringRow)value;
字符串columnName =(字符串)参数;
返回行[columnName];
}
IValueConverter.ConvertBack对象(对象值,类型targetType,对象参数,CultureInfo文化)
{
抛出新的NotImplementedException();
}
}

当我将列表绑定到网格并且可以得到预期的结果时,这可以很好地工作.如果我想更改返回值的颜色,例如"if row [columnName]< 85",那么我需要以红色返回值.默认情况下以黑色返回值.根据条件,我需要更改要返回的文本值的颜色,因为我正在获取需要绑定到网格的值从我传递给convert类的列表中,我无法在其他任何地方实现颜色更改.如何使用converter类实现此目的.

Hi, I have a converter class that implements IValueConverter.

public class ColumnValueSelector: IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
StringRow row = (StringRow)value;
string columnName = (string)parameter;
return row[columnName];
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

This works perfectly fine when I am binding the list to the grid and I am able to get the expcted results.If I want to change the color of the returned value, for example "if row[columnName] < 85" then I need to return the value in red color.By default it returns the value in black color.Depending on the condition I need to change the color of the text value I am returning.since I am getting the values that needs to be binded to the grid from the list I am passing to the convert class, I can''t have the color change implemented anywhere else.how can we do this using the converter class.

推荐答案

您需要在datagrid列中使用转换器,并将您声明的IValueConverter类绑定到xaml页中的列或行,特别是在代码中,代码将如下所示,

RowBackground ="{Binding ElementName = myElement,Path = Value,Converter = {StaticResource ColumnValueSelector}}"
you need to use the converter in your datagrid column and bind the IValueConverter class that you have declared to the column or the row in particular in your xaml page, the code will be something like this,

RowBackground="{Binding ElementName=myElement, Path=Value, Converter={StaticResource ColumnValueSelector}}"


这篇关于使用IValueConverter的Silverlight Datagrid格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆