无法使用嵌套的依赖属性初始化 UserControl
Unable to initialize UserControl with nested Dependency properties
我正在尝试公开嵌套控件的两个依赖属性。在这种情况下 Mask
和 DisplayFormatString
.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:local="clr-namespace:View.UserControls"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" x:Class="View.UserControls.DateTimeEdit"
mc:Ignorable="d" >
<Grid>
<dxe:DateEdit x:Name="localDateEdit" Width="200" MaskType="DateTime"
ShowEditorButtons="True"
Mask ="dd MMM yyyy HH:mm"
DisplayFormatString = "dd MMM yyyy HH:mm"/>
</Grid>
</UserControl>
我已将依赖属性添加到父控件
public partial class DateTimeEdit : UserControl
{
public static readonly DependencyProperty DisplayFormatStringProperty =
DependencyProperty.Register("DisplayFormatString", typeof(string), typeof(DateTimeEdit), new PropertyMetadata(0));
public static readonly DependencyProperty MaskProperty =
DependencyProperty.Register("Mask", typeof(string), typeof(DateTimeEdit), new PropertyMetadata(0));
public string DisplayFormatString
{
get { return (string)GetValue(DisplayFormatStringProperty); }
set { SetValue(DisplayFormatStringProperty, value); }
}
public string Mask
{
get { return (string)GetValue(MaskProperty); }
set { SetValue(MaskProperty, value); }
}
public static void OnDisplayFormatStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as DateTimeEdit).localDateEdit.DisplayFormatString = (string)e.NewValue;
}
public static void OnMaskChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as DateTimeEdit).localDateEdit.Mask = (string)e.NewValue;
}
}
但是,当我尝试在父控件上设置属性时,出现与输入格式不正确有关的异常。 Default value type does not match type of property 'DisplayFormatString'
UserControl 用法。
<userControls:DateTimeEdit
Mask="dd MMM yyyy"
DisplayFormatString="dd MMM yyyy"/>
我是否正确公开了嵌套的依赖属性?
PropertyMetadata(object defaultValue)
constructor 使用其 object
参数作为依赖项 属性 的默认值。这里...
new PropertyMetadata(0)
...您正在传递一个整数作为 String
属性 的默认值。这就是异常的意思。
尝试 null
两个属性:
public static readonly DependencyProperty DisplayFormatStringProperty =
DependencyProperty.Register("DisplayFormatString", typeof(string),
typeof(DateTimeEdit), new PropertyMetadata(null));
public static readonly DependencyProperty MaskProperty =
DependencyProperty.Register("Mask", typeof(string),
typeof(DateTimeEdit), new PropertyMetadata(null));
或""
,如果合适的话。
我正在尝试公开嵌套控件的两个依赖属性。在这种情况下 Mask
和 DisplayFormatString
.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:local="clr-namespace:View.UserControls"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" x:Class="View.UserControls.DateTimeEdit"
mc:Ignorable="d" >
<Grid>
<dxe:DateEdit x:Name="localDateEdit" Width="200" MaskType="DateTime"
ShowEditorButtons="True"
Mask ="dd MMM yyyy HH:mm"
DisplayFormatString = "dd MMM yyyy HH:mm"/>
</Grid>
</UserControl>
我已将依赖属性添加到父控件
public partial class DateTimeEdit : UserControl
{
public static readonly DependencyProperty DisplayFormatStringProperty =
DependencyProperty.Register("DisplayFormatString", typeof(string), typeof(DateTimeEdit), new PropertyMetadata(0));
public static readonly DependencyProperty MaskProperty =
DependencyProperty.Register("Mask", typeof(string), typeof(DateTimeEdit), new PropertyMetadata(0));
public string DisplayFormatString
{
get { return (string)GetValue(DisplayFormatStringProperty); }
set { SetValue(DisplayFormatStringProperty, value); }
}
public string Mask
{
get { return (string)GetValue(MaskProperty); }
set { SetValue(MaskProperty, value); }
}
public static void OnDisplayFormatStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as DateTimeEdit).localDateEdit.DisplayFormatString = (string)e.NewValue;
}
public static void OnMaskChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as DateTimeEdit).localDateEdit.Mask = (string)e.NewValue;
}
}
但是,当我尝试在父控件上设置属性时,出现与输入格式不正确有关的异常。 Default value type does not match type of property 'DisplayFormatString'
UserControl 用法。
<userControls:DateTimeEdit
Mask="dd MMM yyyy"
DisplayFormatString="dd MMM yyyy"/>
我是否正确公开了嵌套的依赖属性?
PropertyMetadata(object defaultValue)
constructor 使用其 object
参数作为依赖项 属性 的默认值。这里...
new PropertyMetadata(0)
...您正在传递一个整数作为 String
属性 的默认值。这就是异常的意思。
尝试 null
两个属性:
public static readonly DependencyProperty DisplayFormatStringProperty =
DependencyProperty.Register("DisplayFormatString", typeof(string),
typeof(DateTimeEdit), new PropertyMetadata(null));
public static readonly DependencyProperty MaskProperty =
DependencyProperty.Register("Mask", typeof(string),
typeof(DateTimeEdit), new PropertyMetadata(null));
或""
,如果合适的话。