繁体   English   中英

如何在Xceed PropertyGrid中使用日期选择器?

[英]How to use Date Picker in Xceed PropertyGrid?

我们正在使用Extended WPF Toolkit来实现PropertyGrid

默认日期选择控件似乎不是WPF DatePicker ,而是一个自定义控件,如果我没有记错的话。

通常,我们使用DatePicker控件来选择日期。 是否也可以将它们用于PropertyGrid控件? 为了提供一致的dd.MM.yyyy日期格式,我们需dd.MM.yyyy并且由于此属性是日期,因此也不应显示时间。

可以使用Xceed属性网格完成此操作吗?

[Category("General")]
[DisplayName("Date")]
[PropertyOrder(2)]
public DateTime? Date { get; set; }

日期选择器

您所要求的并不是很难实现的:Xceed PropertyGrid具有高度可定制性,并且可以使用ITypeEditor接口和Editor属性来定制属性编辑

首先,我们需要定义一个自定义编辑器控件:

public class DateTimePickerEditor : DateTimePicker, ITypeEditor
{
    public DateTimePickerEditor()
    {
        Format = DateTimeFormat.Custom;
        FormatString = "dd.MM.yyyy";

        TimePickerVisibility = System.Windows.Visibility.Collapsed;
        ShowButtonSpinner = false;
        AutoCloseCalendar = true;
    }

    public FrameworkElement ResolveEditor(PropertyItem propertyItem)
    {
        Binding binding = new Binding("Value");
        binding.Source = propertyItem;
        binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;

        BindingOperations.SetBinding(this, ValueProperty, binding);
        return this;
    }
}

构造函数中的所有内容都是为了获得特定的行为(即没有时间控件,特定的日期格式等)而设计的。

现在,我们需要将DateTimePickerEditor设置为对象属性的默认编辑器(在我们的示例中称为“ Date”):

[Category("General")]
[DisplayName("Date")]
[PropertyOrder(2)]
[Editor(typeof(DateTimePickerEditor), typeof(DateTimePicker))]
public Nullable<DateTime> Date

希望对您有所帮助。

您还可以使用仅XAML解决方案,使用“带有DataTemplates的自定义编辑器”中显示的编辑器模板:

https://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid&referringTitle=Home

使用这种方法,您不会使模型类与外部库的属性杂乱无章。

您可以通过以下方式实现日期时间类型的格式化输入:

<xctk:PropertyGrid>
    <xctk:PropertyGrid.EditorDefinitions>    
        <xctk:EditorTemplateDefinition>
            <xctk:EditorTemplateDefinition.TargetProperties>
                <xctk:TargetPropertyType Type="{x:Type sys:DateTime}" />
            </xctk:EditorTemplateDefinition.TargetProperties>
            <xctk:EditorTemplateDefinition.EditingTemplate>
                <DataTemplate>
                    <xctk:DateTimePicker Value="{Binding Value}" Format="ShortDate" />
                </DataTemplate>
            </xctk:EditorTemplateDefinition.EditingTemplate>
       </xctk:EditorTemplateDefinition>
   </xctk:PropertyGrid.EditorDefitions>
</xctk:PropertyGrid>

使用命名空间sys定义的xmlns:sys="clr-namespace:System;assembly=mscorlib"

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM