背景。WPF datagrid单元格颜色取决于preivous单元值
我正在开发股票交易应用程序。 明显有市场观察。 我正在开发这个市场手表使用Datagrid
。
网格是做什么的? 它显示股票的价格点。 每当股票价格上涨时,特定单元格前景变为绿色,如果它减少,它将变为红色。
我做了什么? 我试图使用值转换器方法和多重绑定
问题。 值转换器只给出当前值。 我如何将旧值传递给该转换器。
代码:
<wpfTlKit:DataGrid.CellStyle>
<Style TargetType="{x:Type wpfTlKit:DataGridCell}">
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource myHighlighterConverter}"
>
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}"></Binding>
<Binding Path="Row" Mode="OneWay"></Binding>
<Binding ElementName="OldData" Path="Rows"></Binding>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</wpfTlKit:DataGrid.CellStyle>
转换
public class HighlighterConverter : IMultiValueConverter
{
#region Implementation of IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[1] is DataRow)
{
//Change the background of any cell with 1.0 to light red.
var cell = (DataGridCell)values[0];
var row = (DataRow)values[1];
var columnName = cell.Column.SortMemberPath;
if (row[columnName].IsNumeric() && row[columnName].ToDouble() == 1.0)
return new SolidColorBrush(Colors.LightSalmon);
}
return SystemColors.AppWorkspaceColor;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
#endregion
}
public static class Extensions
{
public static bool IsNumeric(this object val)
{
double test;
return double.TryParse(val.ToString(), out test);
}
public static double ToDouble(this object val)
{
return Convert.ToDouble(val);
}
}
感谢,但你可以给我提供一个例子。是WPF的新手,我不明白如何将对象绑定到数据网格。 – Megatron 2011-05-29 08:19:54
我不认为'DataRowVersion'是解决方案。当您需要访问从数据库接收的版本和本地修改版本时,这非常有用。 OP要的是数据库的旧值和数据库的新值。 – svick 2011-05-29 11:45:36