0

There is a property of type DateTime in a propertygrid. Here is a code:

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

When app is started I see current date instead of required 7.7.2020. Changing of the property temp.Date doesn't reflected in propertygrid. The following code doesn't lead to the result:

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? Thanks for assistance.

1 Answers1

0

I reproduced it with your code, and it works for me. Here is my code:

Main window 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. 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. 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.

Make sure you set the DataContext to the code-behind.

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). Simply in your BindingOperations.SetBinding method change DateTimeUpDown.TextProperty to 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.

Community
  • 1
  • 1
J. Swietek
  • 405
  • 3
  • 13
  • Thanks! I replaced `BindingOperations.SetBinding(temp1, DateTimeUpDown.TextProperty, _binding);` with `BindingOperations.SetBinding(temp1, DateTimeUpDown.ValueProperty, _binding);` and everything is working fine now. – Dimon_koem Oct 13 '15 at 11:10