Propertychanged Template10 Class - c#

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

Related

Nested property for user control

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); }
}

How to Expose multiple DependencyProperties (with same Name) of nested Controls in a UserControl?

I tried to solve almost same problem: "How to Expose a DependencyProperty of a Control nested in a UserControl?"
The difference is that I have different (2 or more) nested Controls with of the same type. My goal is to make the nested DependencyProperties bindable. The main Problem I am facing is that Binding don't uses the the Getter and Setter of the CLR-Property but the String of the registererd DependencyProperty. With 2 (or more) nested Controls I am facing a naming conflict.
To illustrate my Problem here the Code of the outer UserControl:
public partial class OuterControl : UserControl
{
public OuterControl()
{
InitializeComponent();
}
public Visibility PropOfInnerControl
{
get { return (Visibility)GetValue(PropOfInnerControlProperty); }
set { SetValue(PropOfInnerControlProperty, value); }
}
// Using a DependencyProperty as the backing store for UserControl2Visibility. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PropOfInnerControlProperty =
InnerControl.PropOfInnerControlProperty.AddOwner(typeof(OuterControl), new FrameworkPropertyMetadata(MyVisibilityPropertyChanged));
private static void MyVisibilityPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var OuterControl = obj as OuterControl;
OuterControl.InnerControl1.PropOfInnerControl = (Visibility)e.NewValue;
}
}
If it's not clear: In my example the DependencyProperty of the InnerControl1 is called PropOfInnerControlProperty and is registerd under the String "PropOfInnerControl".
In my example Binding with the InnerControl1 works fine. But I don't know how to resolve the same Problem with the InnerControl2.

databinding dictionary <string,int> to a dependency property

I need some help!
I have build a custom control and added to it a DependencyProperty of type:
Dictionary<string,int>
and from the XAML where the control is held I do a data binding to bind to the Dictionary outside the control.
here are some code snippets:
View model of the control who hold the custom control
private Dictionary<string, int> _wordsList;
public Dictionary<string, int> WordsList
{
get
{
return _wordsList;
}
set
{
_wordsList = value;
RaisePropertyChanged("WordsList");
}
}
public WordsViewModel()
{
//CalculateWordsDictionary returns a dictionary<string,int>
WordsList = CalculateWordsDictionary(texts);
}
XAML:
<local:MyControl WordsList="{Binding Path=WordsList}" />
Code behind of the custom control:
public Dictionary<string, int> WordsList
{
get { return (Dictionary<string, int>)GetValue(WordsListProperty); }
set { SetValue(WordsListProperty, value); }
}
// Using a DependencyProperty as the backing store for WordsList. This enables animation, styling, binding, etc...
public static readonly DependencyProperty WordsListProperty =
DependencyProperty.Register("WordsList", typeof(Dictionary<string, int>), typeof(MyControl), new PropertyMetadata(new Dictionary<string, int>()));
I've set a break point on the set of the DependencyProperty and it never reaches this point.
I just can't figure out why this isn't working... or maybe there is some other way to pass the dictionary to the control?
btw
I am using MVVM Light
the important stuff you did not post:) whats your binding within your usercontrol? you have to use some kind of "local binding" so that your MyControl binds to it dependency property. you can use ElementName binding like this:
EDIT: here is the code for the UserControl called MyControl (just a snippet)
<MyControl x:Name=uc>
<ContentControl Content="{Binding ElementName=uc, Path=WordsList}"/>
Fixed!!!!!
All the comments together was the question
I there was a DataContext defined, I removed it completely. Also I registered the property changed callback
worked as charm!
Thanks to all

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.

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.

Categories

Resources