您只能在DependencyProperty
支持的属性上使用数据绑定。例如,如果您查看TextBlock
的文档,您会发现Text
属性与DependencyProperty
类型的公共静态字段具有匹配的TextProperty
。
如果您查看Page
的文档,您会发现没有定义TitleProperty
,因此Title
属性不是依赖项属性。
编辑
有没有办法 “越权” 这可是你可以创建一个附加属性: -
public static class Helper
{
#region public attached string Title
public static string GetTitle(Page element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
return element.GetValue(TitleProperty) as string;
}
public static void SetTitle(Page element, string value)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
element.SetValue(TitleProperty, value);
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.RegisterAttached(
"Title",
typeof(string),
typeof(Helper),
new PropertyMetadata(null, OnTitlePropertyChanged));
private static void OnTitlePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Page source = d as Page;
source.Title = e.NewValue as string;
}
#endregion public attached string Title
}
现在你的页面的XAML可能看起来有点像: -
<navigation:Page ...
xmlns:local="clr-namespace:SilverlightApplication1"
local:Helper.Title="{Binding Name}">
啊我明白了。我假设没有办法重写这个? – zXynK 2010-04-18 19:20:21
@zXynK:附加属性可能适用于您的情况,编辑答案以显示如何完成。 – AnthonyWJones 2010-04-18 20:07:26
感谢您的帮助。 – zXynK 2010-04-19 20:19:43