2013-03-02 70 views
2

由于Windows8还没有充实的DatePicker,我决定遵循一些例子在那里滚动我自己的。用户控件绑定将值传递给一个属性

本身它工作正常,但现在我有日期,我想预先填充DatePicker。

我创造了DatePicker.xaml.cs属性如下文件:

public DateTime dateVal 
{ 
    get 
    { 
     return m_dateVal; 
    } 
    set 
    { 
     m_dateVal = value; 
    } 
} 

然后在所显示的DatePicker控制我的网页我试图绑定属性:

<dp:DatePicker Foreground="Black" Height="100" Margin="10,25" Grid.Column="1" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" dateVal="{Binding repairInfoSingle.repairDate, Mode=TwoWay}"/> 

但是,进入DatePicker.xaml.cs文件时,dateVal属性从未填充过我传入的日期。

然后我得到的输出窗口中的错误:

WinRT的信息:无法分配财产 'aG.Common.DatePicker.dateVal'。 [线路:125职位:170]

我希望通过日期,以便然后在构造函数然后我可以通过解析出来的月,日和年设置SelectedIndex值。

回答

4

如果要绑定到属性(例如,使用DateVal={Binding ...}) - DateVal不能是常规的CLR属性。
你需要将其更改为DependencyProperty

所以,在你的榜样:

public DateTime DateVal 
{ 
    get { return (DateTime) GetValue(DateValProperty); } 
    set { SetValue(DateValProperty, value); } 
} 

public static readonly DependencyProperty DateValProperty = 
    DependencyProperty.Register("DateVal", typeof(DateTime), typeof(DatePicker), 
    new PropertyMetadata(DateTime.MinValue)); 

现在应该很好地工作像你想:

<dp:DatePicker DateVal="{Binding repairInfoSingle.repairDate, Mode=TwoWay}"/> 
2

如果你想绑定值为dateVal您必须在DatePicker.xaml.cs

中制作 dateVal a
public DateTime DateVal 
    { 
     get { return (DateTime)GetValue(DateValProperty); } 
     set { SetValue(DateValProperty, value); } 
    } 

    public static readonly DependencyProperty DateValProperty = 
     DependencyProperty.Register("DateVal", typeof(DateTime), typeof(DatePicker), new PropertyMetadata(DateTime.MinValue)); 
相关问题