日期时间网格字段的WPF IValueConverter
我有一个xamDataGrid,可以从文件中读取数据。此字段的类型为datetime。当加载的日期大于今天的日期时,我想更改此列的前景色。我有以下几点建议:日期时间网格字段的WPF IValueConverter,wpf,ivalueconverter,Wpf,Ivalueconverter,我有一个xamDataGrid,可以从文件中读取数据。此字段的类型为datetime。当加载的日期大于今天的日期时,我想更改此列的前景色。我有以下几点建议: public class MyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo cultu
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
var color = new SolidColorBrush(Colors.Red);
int result = DateTime.Compare((DateTime)value, todaysDate);
if (result > 0)
return color = new SolidColorBrush(Colors.Red);
else
return color = new SolidColorBrush(Colors.White);
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
我从这个:(DateTime)值中得到了一个错误
当我逐步浏览代码时,似乎执行了if(value!=null),即使datagrid中没有显示数据。不确定为什么会出现这种情况。它应该只在我将文件加载到datagrid时执行
以下是xaml:
<viewModel:MyConverter x:Key="myDateConv"/>
<igDP:UnboundField Name="My Date" BindingPath="MyDate" Width="Auto" BindingMode="TwoWay" >
<igDP:UnboundField.Settings>
<igDP:FieldSettings AllowEdit="True" >
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type igEditors:XamDateTimeEditor}" >
<Setter Property="Mask" Value="mm/dd/yyyy" />
<Setter Property="Foreground" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Converter={StaticResource myDateConv}}"/>
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
</igDP:UnboundField.Settings>
</igDP:UnboundField>
注意,Convert方法中的局部变量
color
是完全冗余的。只需写入返回结果>0?画笔。红色:画笔。白色
。此外,如果(DateTime)值
引发InvalidCastException,值
显然不是DateTime,因此绑定的源属性不是DateTime;public DateTime MyDate{get{return MyDate;}set{if(MyDate!=value){MyDate=value;NotifyPropertyChanged(“MyDate”);}}}}}}如何向注释添加格式正确的代码?我也想添加我的xaml?编辑问题。。。
public class myPrice : INotifyPropertyChanged
{
private DateTime myDate;
public DateTime MyDate
{
get { return myDate; }
set
{
if (myDate != value)
{
priceDate = value;
NotifyPropertyChanged("MyDate");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}