【发布时间】:2015-07-04 01:05:40
【问题描述】:
我需要通过 Windows Phone 8 中的绑定表达式来设置 RichTextBox 用户控件的 Xaml 属性,我发现它不是 DP,所以我决定从 RichTextBox 继承并添加一个会改变的 DP带有 PropertyChanged 事件的 Xaml 属性,无论如何代码看起来像这样,去掉了不相关的部分。
public class RichTextBoxWithBindableXaml : RichTextBox
{
public string BindableXaml
{
get { return (string)GetValue(BindableXamlProperty); }
set { SetValue(BindableXamlProperty, value); }
}
public static readonly DependencyProperty BindableXamlProperty =
DependencyProperty.Register("BindableXaml",
typeof(string),
typeof(RichTextBoxWithBindableXaml),
new PropertyMetadata(0));
}
//xaml code
<local:RichTextBoxWithBindableXaml BindableXaml="{Binding PageContent , Mode=OneWay}"> </local:RichTextBoxWithBindableXaml>
我收到以下可怕的异常消息: “System.Windows.Data.Binding”类型的对象无法转换为“System.String”类型。
我已经检查了许多针对这些异常和数据绑定的类似问题的解决方案,并且仍在查看右侧建议的类似问题,但仍然无法理解为什么一个简单的事情对我不起作用。我上面列出的代码只是带有绑定表达式的 DP 的最简单实现。顺便说一句,源 PageContent 来自 INotifyPropertyChanged 对象,我知道它可以工作,因为它可以绑定到 TextBlock 的 Text 属性。
我错过了这么明显的东西吗?我不想为这么简单的事情发布问题,但我似乎无法以任何方式解决。
编辑: 以下 P.S note 被证明是完全无关的。
附:我最后的疑问是加载 xmlns 命名空间本地的方式。它作为 clr 程序集加载,xaml 解析器是否会将我的自定义继承类视为 clr-only 并混淆,因为 clr 属性不是依赖属性。希望这听起来不愚蠢,我很绝望。就是这样:
xmlns:local="clr-namespace:RumCli"
【问题讨论】:
标签: windows-phone-8 dependency-properties data-binding