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

本文关键字:属性 更新 新问题 DateTime PropertyGrid 扩展 工具包 WPF | 更新日期: 2023-09-27 18:17:37

在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的变化?

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

我用你的代码复制了它,它对我有效。下面是我的代码:

主窗口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属性。我明白了,你已经在你的"模型"上实现了INotifyPropertyChanged,所以你不应该需要_propertyGrid.Update(),因为绑定应该为你处理更新。

确保将DataContext设置为代码后置

<标题>编辑

看了你的评论后,我看了看ResolveEditor方法,我想我有解决方案(在我的机器上测试了它,它有效)。只需在BindingOperations.SetBinding方法中将DateTimeUpDown.TextProperty更改为DateTimeUpDown.ValueProperty。我不知道为什么会这样(找不到文档),但我认为文本属性的值被值的值覆盖,在调用SetBinding时设置为null。