Binding DependencyProperty in nested user controls

二次信任 提交于 2019-12-11 20:10:00

问题


I've this problem: I've made 2 user controls: NestedControl1 and NestedControl2. NestedControl2 contains NestedControl1, and NestedControl1 contains just a TextBlock. I've set each NestedControl* DataContext to Self, and created a dependency property for each one. NestedControl1.MyText1 and NestedControl2.MyText2. Then I've bound the NestedControl2.MyText1 to MyText2, and the TextBlock.Text to MyText1 .

If I use the NestedControl2 on a Window and set MyText2 to whatever, it does not work. However if I use directly the NestedControl1 on a Window, it does work. The point is that I would like to make the value of MyText2 arrive to the TextBlock.Text property inside of NestedControl1 .

The code is the following.. What's wrong?? Any idea?? Thank tou in advance for the answers

NestedControl2 code:

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

    public static readonly DependencyProperty MyText2Property = DependencyProperty.Register(
        "MyText2", typeof(string), typeof(NestedControl2), new PropertyMetadata(default(string)));

    public string MyText2
    {
        get { return (string)GetValue(MyText2Property); }
        set { SetValue(MyText2Property, value); }
    }
}

NestedControl2 xaml:

<UserControl x:Class="TestNestedPropertiesWpf.NestedControl2"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:testNestedPropertiesWpf="clr-namespace:TestNestedPropertiesWpf"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
          DataContext="{Binding RelativeSource={RelativeSource Self}}"
         HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid>
    <testNestedPropertiesWpf:NestedControl1 MyText1="{Binding MyText2}" />
</Grid>

NestedControl1 code:

public partial class NestedControl1 : UserControl
{
    public NestedControl1()
    {
        InitializeComponent();

    }


    public static readonly DependencyProperty MyText1Property = DependencyProperty.Register(
        "MyText1", typeof(string), typeof(NestedControl1), new PropertyMetadata(default(string)));

    public string MyText1
    {
        get { return (string)GetValue(MyText1Property); }
        set { SetValue(MyText1Property, value); }
    }
}

NestedControl1 xaml:

<UserControl x:Class="TestNestedPropertiesWpf.NestedControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:testNestedPropertiesWpf="clr-namespace:TestNestedPropertiesWpf"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <TextBlock Text="{Binding MyText1}" 
               x:Name="textBlock" Foreground="Red" 
               Width="300" Height="100" Background="Black"></TextBlock>
</Grid>

And in the end, this is MainWindow.xaml:

<Window x:Class="TestNestedPropertiesWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:testNestedPropertiesWpf="clr-namespace:TestNestedPropertiesWpf"
    Title="MainWindow" Height="350" Width="525"  DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
    <testNestedPropertiesWpf:NestedControl1 MyText1="WORKING"/>
    <testNestedPropertiesWpf:NestedControl2 MyText2="NOT WORKING"/>
</StackPanel>


回答1:


This won't work:

testNestedPropertiesWpf:NestedControl1 MyText1="{Binding MyText2}"

add a clr property to deliver the value from one DP to another:

  • add a .Net property "MyBindableText" to NestedControl2, make the DP MyText1 bind to it.
  • add an OnPropertyChanged handler in the MyText2 DP's registration. In this handler, assign the new value of the DP to "MyBindableText" Now If you set MyText2="bla", the value is forwarded to Mytext1, and will be set for both DP's MyText1 and MyText2



回答2:


I found the solution. The NestedControl2 had DataContext to self, and used NestedControl1, wich had DataContext to self, so the two DataContext were different. To solve the problem I modified the binding declared in the MyText1 like this:

<testNestedPropertiesWpf:NestedControl1 MyText1="{Binding RelativeSource={RelativeSource AncestorType={x:Type testNestedPropertiesWpf:NestedControl2}}, Path=MyText2}" />


来源:https://stackoverflow.com/questions/26991890/binding-dependencyproperty-in-nested-user-controls

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