zXy*_*ynK 6 silverlight silverlight-3.0
XAML:
<navigation:Page ... Title="{Binding Name}">
Run Code Online (Sandbox Code Playgroud)
C#
public TablePage()
{
this.DataContext = new Table()
{
Name = "Finding Table"
};
InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)
在标题绑定发生的位置在InitializeComponent中获取ag_e_parser_bad_property_value错误.我尝试添加静态文本,工作正常.如果我在其他地方使用绑定,例如:
<TextBlock Text="{Binding Name}"/>
Run Code Online (Sandbox Code Playgroud)
这也不起作用.
我猜它是抱怨的,因为没有设置DataContext对象,但是如果我在InitializeComponent之前放入一个断点,我可以确认它已经填充并且设置了Name属性.
有任何想法吗?
您只能对受支持的属性使用数据绑定DependencyProperty.例如,如果您查看文档,TextBlock您会发现该Text属性具有匹配的TextProperty公共静态字段类型DependencyProperty.
如果您查看文档,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
}
Run Code Online (Sandbox Code Playgroud)
现在您的页面xaml可能看起来有点像: -
<navigation:Page ...
xmlns:local="clr-namespace:SilverlightApplication1"
local:Helper.Title="{Binding Name}">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1143 次 |
| 最近记录: |