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