2013-03-02 69 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)); 
相關問題