背景
我正在开发一个股票交易应用程序。
这显然有市场观察。
我正在使用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);
}
}
答案 0 :(得分:4)
要更改DataGrid单元格中的颜色,我建议如下:
构建一个实现INotifyPropertyChanged的模型,该模型包含当前和之前的价格以及反映价格变化的属性(我已在本答案末尾附上完整模型)。
public double ChangeInPrice
{
get
{
return CurrentPrice - PreviousPrice;
}
}
使用转换器根据价格变化在DataGrid中设置CellTemplate的背景。 注意:当价格值发生变化时,INotifyPropertyChanged有助于更改单元格的颜色。
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Path=CurrentPrice}"
Background="{Binding Path=ChangeInPrice, Converter={StaticResource backgroundConverter}}" >
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
[ValueConversion(typeof(object), typeof(SolidBrush))]
public class ObjectToBackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush b = Brushes.White;
try
{
double stock = (double)value;
if (stock > 0)
{
b = Brushes.Green;
}
else if (stock < 0)
{
b = Brushes.Red;
}
}
catch (Exception e)
{
}
return b;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
以下是完整性的完整模型:
public class Stock : INotifyPropertyChanged
{
public Stock(string stockName, double currentPrice, double previousPrice)
{
this.StockName = stockName;
this.CurrentPrice = currentPrice;
this.PreviousPrice = previousPrice;
}
private string _stockName;
public String StockName
{
get { return _stockName; }
set
{
_stockName = value;
OnPropertyChanged("StockName");
}
}
private double _currentPrice = 0.00;
public double CurrentPrice
{
get { return _currentPrice; }
set
{
_currentPrice = value;
OnPropertyChanged("CurrentPrice");
OnPropertyChanged("ChangeInPrice");
}
}
private double _previousPrice = 0.00;
public double PreviousPrice
{
get { return _previousPrice; }
set
{
_previousPrice = value;
OnPropertyChanged("PreviousPrice");
OnPropertyChanged("ChangeInPrice");
}
}
public double ChangeInPrice
{
get
{
return CurrentPrice - PreviousPrice;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
答案 1 :(得分:-1)
我认为问题不是数据网格,而是绑定的对象。如果绑定到数据表,则构建旧值(DataRowVersion)。如果您有其他实体对象,则此实体需要支持原始值和修改后的值。