【发布时间】:2011-09-04 04:15:07
【问题描述】:
背景。
我正在开发一个股票交易应用程序。
其中显然有市场观察。
我正在使用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);
}
}
【问题讨论】: