简体   繁体   English

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

[英]WPF extended toolkit PropertyGrid DateTime property updating issue

There is a property of type DateTime in a propertygrid. propertygrid中有一个DateTime类型的属性。 Here is a code: 这是一个代码:

XAML XAML

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

</xctk:PropertyGrid>

C# 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;
        }

When app is started I see current date instead of required 7.7.2020. 应用启动时,我看到的是当前日期,而不是要求的7.7.2020。 Changing of the property temp.Date doesn't reflected in propertygrid. 属性temp.Date的更改不会反映在propertygrid中。 The following code doesn't lead to the result: 以下代码不会导致结果:

C# C#

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

What should be done to reflect the changes of tempDate in propertygrid? 应该采取什么措施来反映tempDate在propertygrid中的变化? Thanks for assistance. 感谢您的协助。

I reproduced it with your code, and it works for me. 我用您的代码复制了它,并且对我有用。 Here is my code: 这是我的代码:

Main window XAML: 主窗口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>

Code-behind: 后台代码:

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

The only difference is that I don't have a [Editor(typeof(FirstNameEditor), typeof(FirstNameEditor))] attribute on my Date property. 唯一的区别是,我的Date属性没有[Editor(typeof(FirstNameEditor), typeof(FirstNameEditor))]属性。 I would check if that doesn't mess with your data. 我会检查是否不会干扰您的数据。

Also, if you're using binding on SelectedObject property, I think it should be bound to Date property in your code-behind. 另外,如果您在SelectedObject属性上使用绑定,则我认为应该将其绑定到后台代码中的Date属性。 I see, you already have INotifyPropertyChanged implemented on your "model", so you shouldn't need a _propertyGrid.Update() , because Binding should handle the updates for you. 我知道,您已经在“模型”上实现了INotifyPropertyChanged ,因此您不需要_propertyGrid.Update() ,因为Binding应该为您处理更新。

Make sure you set the DataContext to the code-behind. 确保将DataContext设置为隐藏代码。

EDIT 编辑

After reading your comment, I took a look at the ResolveEditor method and I think I have solution (tested it on my machine and it works). 阅读您的评论后,我看了看ResolveEditor方法,我认为我已经解决了(在我的机器上对其进行了测试并且可以工作)。 Simply in your BindingOperations.SetBinding method change DateTimeUpDown.TextProperty to DateTimeUpDown.ValueProperty . 只需在BindingOperations.SetBinding方法BindingOperations.SetBinding DateTimeUpDown.TextProperty更改为DateTimeUpDown.ValueProperty I'm not sure why it's that way (couldn't find documentation), but i think that the Text property's value is overriden by the Value's value, which is set to null at the time of calling the SetBinding . 我不确定为什么会这样(找不到文档),但是我认为Text属性的值被Value的值覆盖,该值在调用SetBinding时设置为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 扩展的WPF工具包PropertyGrid PasswordEditor - Extended WPF Toolkit PropertyGrid PasswordEditor WPF扩展工具包PropertyGrid-组成SelectedObject - WPF Extended Toolkit PropertyGrid - Compose SelectedObject 具有自定义属性的扩展WPF工具包PropertyGrid - Extended WPF Toolkit PropertyGrid with Custom Attributes 扩展的WPF工具包PropertyGrid IsExpandable模板 - Extended WPF Toolkit PropertyGrid IsExpandable Template 将ViewModel直接绑定到扩展WPF工具包PropertyGrid - Binding a ViewModel directly to an Extended WPF Toolkit PropertyGrid 如何使用 C# 中的另一个(嵌套)编辑器为扩展 WPF 工具包 PropertyGrid 创建自定义属性编辑器? - How to create a custom property editor for Extended WPF Toolkit PropertyGrid with another (nested) editor in C#? PropertyGrid(扩展WPF工具包)中没有xaml的多行字符串? - Multi-line string in PropertyGrid (Extended WPF toolkit) without xaml? WPF 扩展工具包 PropertyGrid - 默认展开所有属性 - WPF Extended Toolkit PropertyGrid - Expand all properties by default 我可以将RangeSlider(来自WPF扩展工具包)绑定到ObservableCollection的datetime属性吗? - Can I bind a RangeSlider (from WPF extended toolkit) to a datetime property of ObservableCollection? 扩展的WPF工具包RichTextBox Horizo​​ntalScrollBarVisibility问题 - Extended WPF Toolkit RichTextBox HorizontalScrollBarVisibility issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM