如何将自定义附加属性绑定到代码后面的CheckBox.IsChekedProperty?

时间:2016-04-26 11:44:27

标签: c# win-universal-app

我有附加属性,我想将它绑定到CheckBox.IsCheckedProperty。

  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中绑定附加属性的正确方法是什么?

1 个答案:

答案 0 :(得分:-1)

我不明白你的用例是什么? 您可以在xaml中设置attachmentProperty并将其绑定到某个源:

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