Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ag_e_解析器_bad_属性_值Silverlight绑定页面标题_Silverlight_Silverlight 3.0 - Fatal编程技术网

ag_e_解析器_bad_属性_值Silverlight绑定页面标题

ag_e_解析器_bad_属性_值Silverlight绑定页面标题,silverlight,silverlight-3.0,Silverlight,Silverlight 3.0,XAML: 在发生标题绑定时,在InitializeComponent中获取ag_e_parser_bad_property_值错误。我尝试过添加静态文本,效果很好。如果我在其他任何地方使用绑定,例如: public TablePage() { this.DataContext = new Table() { Name = "Finding Table" }; InitializeComponent(); } 这也不行 我猜它在抱怨,因为

XAML:

在发生标题绑定时,在InitializeComponent中获取ag_e_parser_bad_property_值错误。我尝试过添加静态文本,效果很好。如果我在其他任何地方使用绑定,例如:

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

这也不行

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


有什么想法吗?

您只能对
DependencyProperty
支持的属性使用数据绑定。例如,如果查看
TextBlock
的文档,您会发现
Text
属性具有类型为
dependencProperty
的匹配
TextProperty
公共静态字段

如果查看
页面的文档
,您会发现没有定义
标题属性
,因此
标题
属性不是依赖属性

编辑

无法“覆盖”此属性,但可以创建附加属性:-

<TextBlock Text="{Binding Name}"/>
现在,您的页面xaml可能看起来有点像:-

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

}

将以下内容添加到MyPage.xaml.cs:

<navigation:Page ...
    xmlns:local="clr-namespace:SilverlightApplication1"
    local:Helper.Title="{Binding Name}">

将此属性(依赖项属性)添加到代码隐藏后,代码将正常工作。

ah我明白了。我想没有办法覆盖这个?@zXynK:在你的情况下,一个附加属性可能会起作用,编辑答案来显示它是如何完成的。标题不是从属属性真是太傻了。但这是一个很好的解决方案。谢谢。@肯:我同意这是一个令人震惊的疏忽。
<navigation:Page ...
    xmlns:local="clr-namespace:SilverlightApplication1"
    local:Helper.Title="{Binding Name}">
public new string Title
{
  get { return (string)GetValue(TitleProperty); }
  set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register("Title",
      typeof(string),
      typeof(Page),
      new PropertyMetadata(""));