0

在属性网格中有一个 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的变化?感谢您的帮助。

4

1 回答 1

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));
        }
    }
}

唯一的区别是[Editor(typeof(FirstNameEditor), typeof(FirstNameEditor))]我的 Date 属性上没有属性。我会检查这是否不会影响您的数据。

另外,如果您在SelectedObject属性上使用绑定,我认为它应该绑定到Date您的代码隐藏中的属性。我明白了,你已经INotifyPropertyChanged在你的“模型”上实现了,所以你不需要 a _propertyGrid.Update(),因为 Binding 应该为你处理更新。

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

编辑

阅读您的评论后,我查看了该ResolveEditor方法,我认为我有解决方案(在我的机器上测试它并且它有效)。只需在您的BindingOperations.SetBinding方法中更改DateTimeUpDown.TextPropertyDateTimeUpDown.ValueProperty. 我不确定为什么会这样(找不到文档),但我认为 Text 属性的值被 Value 的值覆盖,在调用SetBinding.

于 2015-10-13T10:02:41.087 回答