用于DateTime网格字段的WPF IValueConverter

问题描述 投票:-1回答:1

我有一个xamDataGrid,可从文件中读取数据。此字段的类型为datetime。当加载的日期大于今天的日期时,我想更改此列的前景色。我有以下IValueConverter:

    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)value

当我单步执行代码时,即使在数据网格中没有显示任何数据,也似乎执行了if(value!= null)。不知道为什么要这样。仅当我将文件加载到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>

这里是课程:

   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));
        }
    }
wpf ivalueconverter
1个回答
-1
投票

使用as进行铸造应允许对转换器中的绑定值进行正确的空检查。我建议以这种方式在转换器中进行强制转换,因为它们有时会在您的数据完全填充之前触发并可能导致问题。

此外,您可以清理画笔逻辑。 Clemens建议是最好/最短/最干净的建议,但是如果您不熟悉C#,乍一看也可能很难读懂,所以这是一个中间建议。

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
      //Cast with as to allow null values.
      DateTime? dt = value as DateTime?;

      if (dt != null)
      {          
          if (DateTime.Compare(dt, todaysDate) > 0)
              return Brushes.Red;         
      }

      return Brushes.White; 
}   

这总是返回颜色,但是如果您真的不希望返回任何颜色,请返回DependencyProperty.UnsetValue

© www.soinside.com 2019 - 2024. All rights reserved.