WPF扩展工具包PropertyGrid DateTime属性更新问题

时间:2015-10-13 08:41:15

标签: c# datetime propertygrid wpf-extended-toolkit

在propertygrid中有一个DateTime类型的属性。这是一个代码:

XAML

<xctk:PropertyGrid x:Name="_propertyGrid" Margin="10" AutoGenerateProperties="True" SelectedObject="{Binding}">

</xctk:PropertyGrid>

C#

public class DateEditor : Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor
    {
        public FrameworkElement ResolveEditor(Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem)
        {

            DateTimeUpDown temp1 = new DateTimeUpDown();
            temp1.Format = DateTimeFormat.Custom;
            temp1.FormatString = "dd.MM.yyyy hh:m:ss";

            //create the binding from the bound property item to the editor
            var _binding = new Binding("Value"); //bind to the Value property of the PropertyItem
            _binding.Source = propertyItem;
            _binding.ValidatesOnExceptions = true;
            _binding.ValidatesOnDataErrors = true;
            _binding.Mode = BindingMode.TwoWay;

            BindingOperations.SetBinding(temp1, DateTimeUpDown.TextProperty, _binding);

            return temp1;
        }
    }

public class CustomAttributEditorPerson : INotifyPropertyChanged
    {
        private DateTime FDate;

        [Category("Information")]
        [DisplayName("Date")]
        //This custom editor is a Class that implements the ITypeEditor interface
        [RefreshProperties(RefreshProperties.All)]
        [Editor(typeof(FirstNameEditor), typeof(FirstNameEditor))]
        public DateTime Date
        {
            get
            {
                return this.FDate;
            }
            set
            {
                this.FDate = value;
                NotifyPropertyChanged("Date");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }

public partial class MainWindow : Window
    {
        CustomAttributEditorPerson temp = new CustomAttributEditorPerson();

        public MainWindow()
        {
            InitializeComponent();

            temp.Date = new DateTime(2020, 7, 7, 0, 1, 2);

            _propertyGrid.SelectedObject = temp;
        }

当应用程序启动时,我会看到当前日期而不是7.7.2020。更改属性temp.Date不会反映在propertygrid中。以下代码不会导致结果:

C#

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
              temp.Date = new DateTime(2030, 8, 7, 0, 1, 2);   
              _propertyGrid.Update();        
        }

应该怎样做以反映propertygrid中tempDate的变化? 感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我用你的代码复制了它,它适用于我。 这是我的代码:

主窗口XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:wt="http://schemas.xceed.com/wpf/xaml/toolkit"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <wt:PropertyGrid x:Name="_propertyGrid" Margin="10" AutoGenerateProperties="True" SelectedObject="{Binding}"/>
    <Button Content="!!"  Width="100" Height="100" Click="Button_Click"/>

</Grid>

代码隐藏:

public partial class MainWindow : Window
{
    CustomAttributEditorPerson temp = new CustomAttributEditorPerson();

    public MainWindow()
    {
        InitializeComponent();

        temp.Date = new DateTime(2020, 7, 7, 0, 1, 2);

        _propertyGrid.SelectedObject = temp;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        temp.Date = new DateTime(2030, 8, 7, 0, 1, 2);
        _propertyGrid.Update();
    }
}


public class CustomAttributEditorPerson : INotifyPropertyChanged
{
    private DateTime FDate;

    [Category("Information")]
    [DisplayName("Date")]
    //This custom editor is a Class that implements the ITypeEditor interface
    [RefreshProperties(RefreshProperties.All)]
    public DateTime Date
    {
        get
        {
            return this.FDate;
        }
        set
        {
            this.FDate = value;
            NotifyPropertyChanged("Date");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

唯一的区别是我的Date属性上没有[Editor(typeof(FirstNameEditor), typeof(FirstNameEditor))]属性。我会检查一下你的数据是否混乱。

此外,如果您在SelectedObject属性上使用绑定,我认为它应该绑定到代码隐藏中的Date属性。我知道,您已经在&#34;模型&#34;上实施了INotifyPropertyChanged,因此您不需要_propertyGrid.Update(),因为Binding应该为您处理更新。

确保将DataContext设置为代码隐藏。

修改

阅读完评论后,我看了一下ResolveEditor方法,我认为我有解决方案(在我的机器上进行了测试,并且可行)。 只需在BindingOperations.SetBinding方法中将DateTimeUpDown.TextProperty更改为DateTimeUpDown.ValueProperty即可。我不确定为什么会这样(无法找到文档),但我认为Text属性的值被Value的值覆盖,这是在调用SetBinding时设置为null。