为什么这个绑定更新不?
代码: MainWindow.xaml
<Window x:Class="WpfApplication12.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication12"
Height="350" Width="525">
<StackPanel>
<local:UserControl1 x:Name="usr" />
<TextBlock Text="{Binding ElementName=usr, Path=txt.Text}" />
</StackPanel>
</Window>
UserControl1.xaml
<UserControl x:Class="WpfApplication12.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBox Text="qwe" x:Name="txt" />
</UserControl>
发布于 2011-07-27 05:00:02
TextBox在UserControl中是不可访问的,因为它的保护级别,它也是一个字段,您永远不能绑定到这些。您需要在UserControl后面的代码中将它公开为公共属性。
public TextBox Txt
{
get { return txt; }
}
编辑:正如Holterman指出的那样,可能不想公开整个TextBox,因此可以定义一个依赖属性,例如TextBox内部绑定到的属性。
https://stackoverflow.com/questions/6844682
复制