这似乎是一个直接的任务,我想从MVVM视图传递一个公共属性值,但我不断得到“a”绑定'不能在类型的属性上设置。'绑定'只能在dependencyobject“error。”的依赖属性上设置。
我迭代一个可观察的集合并渲染项目,我想将一些模板代码移动到用户控件中。我怎么能解决这个????
<local:xIPAddressControl UserControlIPAddressText="{Binding Path=IPAddress, RelativeSource={RelativeSource Mode=Self}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
public partial class xIPAddressControl : UserControl
{
public xIPAddressControl()
{
this.InitializeComponent();
}
public string UserControlIPAddressText
{
get { return this.xIPAddressTextBlock.Text; }
set { this.xIPAddressTextBlock.Text = value; }
}
}
答案 0 :(得分:1)
您正在将UserControlIPAddressText定义为CLR属性,但需要在依赖属性系统中注册。
DependencyProperty UserControlIPAddressTextProperty = DependencyProperty.Register("UserControlIPADdressText", typeof(string), null);
答案 1 :(得分:1)
正如评论中所述,您的媒体资源需要DependencyProperty
才能使用Binding
。
以下是您的代码应该是什么样的:
public static readonly DependencyProperty UserControlIPAddressTextProperty=
DependencyProperty.Register("UserControlIPAddressText",
typeof(string),
typeof(xIPAddressControl));
public string UserControlIPAddressText
{
get { return (string)GetValue(UserControlIPAddressTextProperty); }
set { SetValue(UserControlIPAddressTextProperty, value); }
}
答案 2 :(得分:1)
疯狂的事情,我试图错误地设置用户控件。它必须如下。
//this.DataContext = this;
LayoutRoot.DataContext = this;