所以我正在创建这个应用程序,我已经创建了自己的用户控件,并且每个控件都有一些内容将使用来自Web的信息进行更新,但我不知道如何添加属性。就像那个特定控件的名字一样。比如控件中存储的高度设置在哪里?在代码中?喜欢这个
mycontrol:
String nameofthiscontrol = "control";
和主要代码:
mycontrol mycontrol = new mycontrol();
mycontrol.nameofthiscontrol = "control1";
这是怎么回事?我真的需要一些指导,请帮忙!提前谢谢!
答案 0 :(得分:6)
如果您正在谈论UserControl,它将具有所有控件共有的一些基本属性(如宽度,高度,背景等)。您可以在UserControl中添加您在其他任何地方添加的属性。
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
}
//simple property
public DesiredType PropertyName { get; set; }
//dependancy property
public DesiredType MyProperty
{
get { return (DesiredType)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(DesiredType), typeof(ownerclass), new PropertyMetadata(0));
}
两者都很有用(并且必须是公共的),但DependencyProperty 更适合MVVM中的绑定。