【发布时间】:2014-04-07 21:27:22
【问题描述】:
我已经阅读了几篇关于此的文章,但我看不出我在这里做错了什么,谁能帮忙:)
我有一个名为 CreateRuleItemView 的 UserControl 我想在这里添加一个依赖属性,我也可以绑定我的 ViewModel。到目前为止我有。
public partial class CreateRuleItemView : UserControl
{
public CreateRuleItemView()
{
InitializeComponent();
}
public Boolean ShowEditTablePopup
{
get
{
return (Boolean)this.GetValue(ShowEditTablePopupProperty);
}
set
{
this.SetValue(ShowEditTablePopupProperty, value);
}
}
public static readonly DependencyProperty ShowEditTablePopupProperty = DependencyProperty.Register("ShowEditTablePopup", typeof(Boolean), typeof(CreateRuleItemView), new PropertyMetadata(null, OnShowEditTablePopupChanged));
private static void OnShowEditTablePopupChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
}
}
如果我尝试访问用户控件 Xaml 中的属性,我会得到:
<UserControl x:Class="Views.Setup.CreateRuleItemView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DataContext="{d:DesignInstance Type=vm:CreateRuleItemViewModel, IsDesignTimeCreatable=False}"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" ShowEditTablePopup="{Binding DataContext.ShowEditTablePopup}" >
错误 1 成员“ShowEditTablePopup”无法识别或无法访问。
错误 3 类型“UserControl”上不存在属性“ShowEditTablePopup”
错误 2 在类型“UserControl”中找不到属性“ShowEditTablePopup”。
编辑 1: 好的,通过在我设置视图的主窗口后面的代码中添加绑定来解决这个问题。
Setup.CreateRuleItemView v = new Setup.CreateRuleItemView();
BindingOperations.SetBinding(v, CreateRuleItemView.EditTablePopupProperty, new Binding("EditTablePopup"));
【问题讨论】:
标签: c# silverlight user-controls dependency-properties