问题描述
我遇到了这个问题:我已经制作了2个用户控件:NestedControl1和NestedControl2. NestedControl2包含NestedControl1,而NestedControl1仅包含TextBlock.我已经将每个NestedControl * DataContext设置为Self,并为每个对象创建了一个依赖项属性.NestedControl1.MyText1和NestedControl2.MyText2.然后,将NestedControl2.MyText1绑定到MyText2,并将TextBlock.Text绑定到MyText1.
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 .
如果我在窗口上使用NestedControl2并将MyText2设置为任何值,它将无法正常工作.但是,如果我直接在Window上使用NestedControl1,它确实可以工作.关键是我想使MyText2的值到达NestedControl1内部的TextBlock.Text属性.
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 .
代码如下.怎么了???任何的想法??预先感谢tou提供的答案
The code is the following.. What's wrong?? Any idea?? Thank tou in advance for the answers
NestedControl2代码:
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:
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代码:
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:
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>
最后,这是MainWindow.xaml:
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>
推荐答案
我找到了解决方案. NestedControl2拥有自己的DataContext,并且使用NestedControl1拥有自己的DataContext,因此这两个DataContext是不同的.为了解决该问题,我修改了MyText1中声明的绑定,如下所示:
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}" />
这篇关于在嵌套用户控件中绑定DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!