我需要通过在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音符竟然是完全不相干的。
P.S.我最后的疑问是xmlns命名空间local加载的方式。它被加载为clr程序集,xaml解析器可能认为我的自定义继承类为clr-only,并且由于clr属性不是依赖项属性而引起混淆。希望它听起来不笨,我绝望。这是因为这样:
xmlns:local="clr-namespace:RumCli"
有关详细说明,请查看下面的OmegaMan答案。 – bahti