How to Expose multiple DependencyProperties (with same Name) of nested Controls in a UserControl?

♀尐吖头ヾ 提交于 2020-01-04 13:36:25

问题


I tried to solve almost same problem: "How to Expose a DependencyProperty of a Control nested in a UserControl?"

The difference is that I have different (2 or more) nested Controls with of the same type. My goal is to make the nested DependencyProperties bindable. The main Problem I am facing is that Binding don't uses the the Getter and Setter of the CLR-Property but the String of the registererd DependencyProperty. With 2 (or more) nested Controls I am facing a naming conflict.

To illustrate my Problem here the Code of the outer UserControl:

public partial class OuterControl : UserControl
{
    public OuterControl()
    {
        InitializeComponent();
    }

    public Visibility PropOfInnerControl
    {
        get { return (Visibility)GetValue(PropOfInnerControlProperty); }
        set { SetValue(PropOfInnerControlProperty, value); }
    }

    // Using a DependencyProperty as the backing store for UserControl2Visibility.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty PropOfInnerControlProperty =
        InnerControl.PropOfInnerControlProperty.AddOwner(typeof(OuterControl), new FrameworkPropertyMetadata(MyVisibilityPropertyChanged));            

    private static void MyVisibilityPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var OuterControl = obj as OuterControl;
        OuterControl.InnerControl1.PropOfInnerControl = (Visibility)e.NewValue;
    }
}

If it's not clear: In my example the DependencyProperty of the InnerControl1 is called PropOfInnerControlProperty and is registerd under the String "PropOfInnerControl".

In my example Binding with the InnerControl1 works fine. But I don't know how to resolve the same Problem with the InnerControl2.

来源:https://stackoverflow.com/questions/21602378/how-to-expose-multiple-dependencyproperties-with-same-name-of-nested-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!