Nested property for user control - c#

I have created user control and defined all properties. I was able to access/modify the property in design time. Here i need to know how to give nested properties to the same control.
For example consider default property "Font" which have sub properties like "Bold", "Italic" ,"Names" etc. Like this, i need nested properties in my custom control.
Actually i need like this nested properties for user controls in my web application.
Thanks in advance.

Try to change all properties to dependency properties. For example:
public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(Choice));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static DependencyProperty Choice4Property = DependencyProperty.Register("Choice4", typeof(Choice), typeof(MyControl));
public Choice Choice4
{
get { return (Choice)GetValue(Choice4Property); }
set { SetValue(Choice4Property, value); }
}

Related

Propertychanged Template10 Class

The propertychanged trigger in a viewmodel of a UWP app with Template10 is triggered by by the following way:
public var Thing{ get { return thing; } set { Set(ref thing, value); } }
The Set function is placed in the class bindableBase.
How can I use this same function in a Usercontrol?
I tried the folowing, but that didn't work:
BindableBase x;
var foo;
public var Foo{ get { return foo; } set { x.Set(ref foo, value); } }
you don't use in that fashion you use with a viewmodel for example if the Page you place the usercontrol would have a property associated with populating the fields of the usercontrol part of the viewmodel that is bound to the DataContext of the Page. I think you need to review MVVM. Or the viewmodel could be the DataContext of the userControl in question.
When creating a UserControl you'll want to use a DependencyProperty to create bindable properties. It's required to make them behave as expected when using the UserControl inside of an other control (like a Page). DependencyProperties are defined like this:
public int MyProperty
{
get { return (int)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(int), typeof(ownerclass), new PropertyMetadata(0));
They're most easily created using the propdp snippet in Visual Studio.
I recommend giving this MVA course a look (especially the first lesson) on how to create custom controls

Howcome the get/set in dependency property doesn't do anything?

I've created a dependency property like this:
public partial class MyControl: UserControl
{
//...
public static DependencyProperty XyzProperty = DependencyProperty.Register("Xyz",typeof (string),typeof (MyControl),new PropertyMetadata(default(string)));
public string Xyz
{
get { return (string) GetValue(XyzProperty ); }
set { SetValue(XyzProperty , value); }
}
//...
}
Then bind it to my wpf window and everything worked fine.
When I tried to add some logic to the setter I notice it wasn't being called. I modify the get;Set up to a point now they look like this:
get{return null;}
set{}
And it is still works! How come? What's the use of that GetValue/SetValue calls?
The WPF data binding infrastructure uses the DependencyProperty directly, the Xyz property is a convenience interface for the programmer.
Take a look at the PropertyMetadata in the call to DependencyProperty.Register, you can supply a callback that will run when the property value is changed, this is where you can apply your business logic.
The DependencyProperty is the backing store for the XyzProperty. If you access the property through the DependencyProperty interface, it completely bypasses the Property's Get/Set accessor.
Think of it this way:
private int _myValue = 0;
public int MyValue
{
get { return _myValue; }
set { _myValue = value; }
}
In this instance, if I manually assign _myValue = 12, obviously the "Set" accessor for the MyValue property won't be called; I completely bypassed it! The same is true for DependencyProperties. WPF's binding system uses the DependencyProperty interfaces directly.

Specify which Property goes between the opening and closing tag in Xaml

Consider the following Xaml
<Grid>
<TextBox>Text</TextBox>
<Button>Content</Button>
</Grid>
It will set the
Text Property of a TextBox (only WPF)
Content Property of a Button
Children Property of a Grid
But how is this specified? How do you specify which Property that goes between the opening and closing tag in Xaml?
Is this set by some metadata in the Dependency Property or what?
Thanks
There is a ContentPropertyAttribute that is applied to a class. WPF/Silverlight will use reflection to determine which property to use.
If you want to do this with a custom class, you can do it like so:
[ContentProperty("Bar")]
public class Foo : Control
{
public static DependencyProperty BarProperty = DependencyProperty.Register(
"Bar",
typeof(int),
typeof(Foo),
new FrameworkPropertyMetaData(0));
public int Bar
{
get { return (int)GetValue(BarProperty); }
set { SetValue(BarProperty, value); }
}
}
Then you could specify it in XAML like so:
<lcl:Foo>12</lcl:Foo>
Update
Since it is using reflection, you don't really need to do a DependencyProperty. For instance, this will also work:
[ContentProperty("Bar")]
public class Foo : Control
{
public int Bar { get; set; }
}

WPF Dependency Property not working

I have a custom Dependency Property defined like so:
public static readonly DependencyProperty MyDependencyProperty =
DependencyProperty.Register(
"MyCustomProperty", typeof(string), typeof(MyClass));
private string _myProperty;
public string MyCustomProperty
{
get { return (string)GetValue(MyDependencyProperty); }
set
{
SetValue(MyDependencyProperty, value);
}
}
Now I try set that property in XAML
<controls:TargetCatalogControl MyCustomProperty="Boo" />
But the setter in DependencyObject never gets hit! Although it does when I change the property to be a regular property and not a Dep Prop
Try this..
public string MyCustomProperty
{
get
{
return (string)GetValue(MyCustomPropertyProperty);
}
set
{
SetValue(MyCustomPropertyProperty, value);
}
}
// Using a DependencyProperty as the backing store for MyCustomProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyCustomPropertyProperty =
DependencyProperty.Register("MyCustomProperty", typeof(string), typeof(TargetCatalogControl), new UIPropertyMetadata(MyPropertyChangedHandler));
public static void MyPropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// Get instance of current control from sender
// and property value from e.NewValue
// Set public property on TaregtCatalogControl, e.g.
((TargetCatalogControl)sender).LabelText = e.NewValue.ToString();
}
// Example public property of control
public string LabelText
{
get { return label1.Content.ToString(); }
set { label1.Content = value; }
}
It doesn't, unless you call it manually. There's a property-changed handler you can add to the DependancyProperty constructor call to be notified of when the property changes.
Call this constructor:
http://msdn.microsoft.com/en-us/library/ms597502.aspx
With a PropertyMetadata instance created by this constructor:
http://msdn.microsoft.com/en-us/library/ms557327.aspx
EDIT: Also, you are not implementing the dependancy property correctly. Your get and set should use GetValue and SetValue respectively, and you should not have a class member to store the value. The member name of the DP should also be {PropertyName}Property, e.g. MyCustomPropertyProperty if the get/set and property name as registered is MyCustomProperty. See http://msdn.microsoft.com/en-us/library/ms753358.aspx for more information.
Hope that helps.
Maybe you are using MVVM, and overriding the DataContext of your View ?
If you do, then the event for changing MyCustomProperty will be raised on the original DataContext and not on the new ViewModel.

How do I declare a DependencyProperty under the "Visibility" category instead of "Other"?

I have a few DependencyProperties defined in some custom user controls. I'd like for Visual Studio (and Blend as a bonus) to list these properties under the "Visibility" category instead of "Other" or "Miscellaneous".
How can I add some metadata to these properties to make this happen?
Edit: To clarify my question, I guess I want the "normal" get/set properties to show up and not really the static Dependency Properties themselves, so as an example, the property I want to group is ConnectorLabelText:
public string ConnectorLabelText
{
get { return (string)GetValue(ConnectorLabelTextProperty); }
set { SetValue(ConnectorLabelTextProperty, value); }
}
// Using a DependencyProperty as the backing store for ConnectorLabelText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ConnectorLabelTextProperty =
DependencyProperty.Register("ConnectorLabelText", typeof(string), typeof(InputConnector), new UIPropertyMetadata("IN"));
See this question
Adding an example
[Description("My Description"), Category("Visibility")]
public string ConnectorLabelText
{
get { return (string)GetValue(ConnectorLabelTextProperty); }
set { SetValue(ConnectorLabelTextProperty, value); }
}

Categories

Resources