我遇到了这个问题:我创建了2个用户控件:NestedControl1和NestedControl2。 NestedControl2包含NestedControl1,而NestedControl1只包含一个TextBlock。 我已将每个NestedControl * DataContext设置为Self,并为每个创建了一个依赖项属性。 NestedControl1.MyText1和NestedControl2.MyText2。 然后我将NestedControl2.MyText1绑定到MyText2,将TextBlock.Text绑定到MyText1。
如果我在Window上使用NestedControl2并将MyText2设置为任何东西,它就不起作用。但是,如果我直接在Window上使用NestedControl1,它确实有效。关键是我想让MyText2的值到达NestedControl1里面的TextBlock.Text属性。
代码如下..出了什么问题?任何的想法??提前感谢tou的答案
NestedControl2代码:
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代码:
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>
最后,这是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>
答案 0 :(得分:0)
这不会起作用:
testNestedPropertiesWpf:NestedControl1 MyText1="{Binding MyText2}"
添加clr属性以将值从一个DP传递到另一个DP:
答案 1 :(得分:0)
我找到了解决方案。 NestedControl2有自己的DataContext,并使用NestedControl1,它有自己的DataContext,所以两个DataContext是不同的。 为了解决这个问题,我修改了MyText1中声明的绑定,如下所示:
<testNestedPropertiesWpf:NestedControl1 MyText1="{Binding RelativeSource={RelativeSource AncestorType={x:Type testNestedPropertiesWpf:NestedControl2}}, Path=MyText2}" />