Reputation: 799
So i'm creating this app, where i have created my own user controls, and each of the controls has things that will be updated with information from the web, but i dont know how i can add properties to it. Like the name of that specific control. Like where are the height settings for instance stored in the control? In the code? Like this
mycontrol:
String nameofthiscontrol = "control";
and main code:
mycontrol mycontrol = new mycontrol();
mycontrol.nameofthiscontrol = "control1";
Is that how it works? I really need some guiding on this one, please help! Thanks in advance!
Upvotes: 0
Views: 955
Reputation: 864
If you are talking about UserControl, it will have some basic properties common to all controls(like Width, Height, Background and such). You add properties like you would add anywhere else - within your 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));
}
Both are useful(and must be public), but DependencyProperty is better for bindings in MVVM.
Upvotes: 6