2016-04-26 49 views
0

我有附加的屬性,我想將它綁定到CheckBox.IsCheckedProperty。如何將代碼後面的自定義附加屬性綁定到CheckBox.IsChekedProperty?

public static object GetIsCheckedState(DependencyObject obj) 
    { 
     return (object)obj.GetValue(IsCheckedStateProperty); 
    } 

    public static void SetIsCheckedState(DependencyObject obj, object value) 
    { 
     obj.SetValue(IsCheckedStateProperty, value); 
    } 

    // Using a DependencyProperty as the backing store for IsCheckedState. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty IsCheckedStateProperty = 
     DependencyProperty.RegisterAttached("IsCheckedState", typeof(object), typeof(GridCellCheckBoxRenderer), new PropertyMetadata(null,OnIsCheckedStateChanged)); 

    private static void OnIsCheckedStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var checkBox = d as CheckBox; 
     checkBox.IsChecked = (bool?)e.NewValue; 
    } 
uiElement.SetBinding(CheckBox.IsCheckedProperty, new Binding() { Path = new PropertyPath("IsCheckedState"), Source = uiElement}); 

在UWP中綁定類似上面的屬性是否正確?

+0

是的UIElement是CheckBox控件? – Archana

+0

如果是這樣,uiElement.SetBinding(CheckBox.IsCheckedProperty,new Binding(){Path = new PropertyPath(「IsCheckedState」),Source = uiElement})是錯誤的。你給了source = uielement,它是沒有IsCheckedState屬性的複選框,除非你已經在複選框的子類中聲明瞭你的DependencyProperty。請解釋你的用例。需要的郵政編碼 – Archana

+0

是的。在上面的代碼中,uiElement是CheckBox控件。我怎樣才能將我的附加屬性綁定到雙向模式的CheckBox.IsChecked屬性? – Smirti

回答

-1

我不明白你的用例是什麼? 您可以在XAML設置attachedProperty並將其綁定到一些來源:

<Checkbox attProp:AttachedPropertiesClassName.AttachedProperty="<your binding>" /> 
+0

如何在代碼後面做到這一點? – Smirti

相關問題