2016-09-28 48 views
0

我试图公开嵌套控件的两个依赖属性。在这种情况下,MaskDisplayFormatString无法使用嵌套的依赖属性初始化UserControl

<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"/> 

我是否正确地暴露嵌套的依赖属性?

回答

0

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

或者"",如果合适的话。