2016-04-26 69 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