Meg*_*ron 5 wpf datagrid colors cell
背景.
我正在开发一个股票交易应用程序.这显然有市场观察.我正在开发这款市场手表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>
Run Code Online (Sandbox Code Playgroud)
变流器
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);
}
}
Run Code Online (Sandbox Code Playgroud)
bli*_*eis -1
好吧,我认为问题不在于数据网格,而在于您绑定到的对象。如果您绑定到数据表,则内置旧值(DataRowVersion)。如果您有其他实体对象,那么该实体需要支持原始值和修改后的值。
| 归档时间: |
|
| 查看次数: |
6622 次 |
| 最近记录: |