【发布时间】:2012-03-29 17:11:48
【问题描述】:
我有一个用户控件,它的 Title 属性绑定到 TextBlock 的 Text 属性:
XAML
<TextBlock Grid.Column="0" Text="{Binding ElementName=me,Path=Title}" HorizontalAlignment="Left" VerticalAlignment="Top" />
后面的代码
public String Title
{
get { return (String)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(String), typeof(ClosableTabHeader), new UIPropertyMetadata(""));
我希望将此 Title 属性绑定到主窗口上的另一个属性:
XAML
<my:ClosableTabHeader Title="{Binding ElementName=me,Path=ShortenAmount}" />
后面的代码
public Int32 ShortenAmount
{
get { return (Int32)GetValue(ShortenAmountProperty); }
set { SetValue(ShortenAmountProperty, value); }
}
public static readonly DependencyProperty ShortenAmountProperty =
DependencyProperty.Register("ShortenAmount", typeof(Int32), typeof(MainWindow), new UIPropertyMetadata(0));
但是,文本块不会更新。
但是,当我用 TextBlock 替换 usercontrol 声明时,它可以正常工作:
<TextBlock Text="{Binding ElementName=me,Path=ShortenAmount}" />
虽然肯定有解决方法,但就我自己尝试做的事情而言,我可以知道我哪里出错了吗?
【问题讨论】:
-
包含 x:Name=me 的代码在哪里?
标签: .net wpf data-binding user-controls