ag_e_parser_bad_property_value Silverlight绑定页面标题

前端之家收集整理的这篇文章主要介绍了ag_e_parser_bad_property_value Silverlight绑定页面标题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
XAML:

<navigation:Page ... Title="{Binding Name}">

C#

public TablePage()
{
    this.DataContext = new Table() 
    { 
        Name = "Finding Table"
    };
    InitializeComponent();
}

标题绑定发生的位置在InitializeComponent中获取ag_e_parser_bad_property_value错误.我尝试添加静态文本,工作正常.如果我在其他地方使用绑定,例如:

<TextBlock Text="{Binding Name}"/>

这也不起作用.

我猜它是抱怨的,因为没有设置DataContext对象,但是如果我在InitializeComponent之前放入一个断点,我可以确认它已经填充并且设置了Name属性.

有任何想法吗?

解决方法

您只能对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}">

猜你在找的Silverlight相关文章