UserControl with DependencyProperty in MVVM Light - c#

I have a UserControl that add a DependencyProperty for it .
public const string TextValuePropertyName = "TextValue";
public string TextValue
{
get
{
return (string)GetValue(TextValueProperty);
}
set
{
SetValue(TextValueProperty, value);
}
}
public static readonly DependencyProperty TextValueProperty = DependencyProperty.Register(
TextValuePropertyName,
typeof(string),
typeof(FormatUserControl),
new UIPropertyMetadata());
and use it in another Usercontrol
<local:FormatUserControl TextValue="{Binding Subject,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
When i use this don't set value for this property when i change Subject value?

Your FormatUserControl is the class and you should register the property for it not for the NumberFormatUserControl like this (I am not aware what is the relationship between the two user controls):
public static readonly DependencyProperty TextValueProperty = DependencyProperty.Register(
TextValuePropertyName,
typeof(string),
typeof(FormatUserControl),
new UIPropertyMetadata());

Related

Xamarin Bindable property

I've created my own custom control in Xamarin in which I want to set a property from the xaml code.
<MyClass MyProperty="10" />
I've tried to use a BindableProperty in order to get it work, my code behind looks like:
public static readonly BindableProperty BindProp = BindableProperty.Create(
nameof(MyProperty),
typeof(int),
typeof(MyClass),
0);
public int MyProperty
{
get
{
return (int)GetValue(BindProp);
}
set
{
SetValue(BindProp, value);
}
}
I expedted that MyProperty should be initiated to 10. What am I doing wrong?

CollectionPropertiesShouldBeReadOnly and Dependency Properties

I currently have a Dependency Property as such:
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyPropertyDefaults", typeof(ICollection<string>), typeof(MyPanel), new PropertyMetadata(new List<string>()));
public ICollection<string> MyProperty
{
get
{
return GetValue(MyPropertyProperty) as ICollection<string>;
}
set
{
this.SetValue(MyPropertyProperty, value);
}
}
The aim is that this subpanel will be passed a list via a binding, that the subpanel manipulates, and then the parent can read later. E.g.
<xaml:MyPanel MyProperty="{Binding MyPropertyList}" />
However, FxCop reports CollectionPropertiesShouldBeReadOnly and I need to remove the setter, which is required by the property. How do I fix this? What is the correct way to do what I am doing?
Declare the setter as private like this:
public class MyPanel : Panel
{
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyPropertyDefaults", typeof(ICollection<string>), typeof(MyPanel), new PropertyMetadata(new List<string>()));
public ICollection<string> MyProperty
{
get
{
return GetValue(MyPropertyProperty) as ICollection<string>;
}
private set
{
this.SetValue(MyPropertyProperty, value);
}
}
}

Binding to a property in a UserControl

I have a usercontrol in which i want to expose a property called ExpressionText and in the
xaml a binding can be defined to this property.
So i created a dependency property
public static readonly DependencyProperty EditorText =DependencyProperty.Register("EditorText", typeof(string), typeof(MyUerControl));
and
public string ExpressionText
{
get
{
return (string)GetValue(EditorText);
}
set
{
SetValue(EditorText, value);
}
}
in the xaml i do this.
<controls:MyUerControl x:Name="textEditor" ExpressionText="{Binding
Path=Expression,Mode=TwoWay}" />
but i get
A binding cannot be set on ExpressionText property of type MyUserControl. Binding can be set
only on a depenedecy property of type Dependency object error.
Is there something wrong in my approach ? How do i solve this problem ?
This should work:
public static DependencyProperty EditorTextProperty = DependencyProperty.Register("ExpressionText", typeof(string), typeof(MyUserControl),
new PropertyMetadata(new PropertyChangedCallback((s, e) =>
{ })));
public string ExpressionText
{
get
{
return (string)base.GetValue(EditorTextProperty);
}
set
{
base.SetValue(EditorTextProperty, value);
}
}
You are defining EditorText as the name of your DependencyProperty. That is the name that is available publicly for you to bind to. If you want it to be called ExpressionText, then you need to register that as the name.
public static readonly DependencyProperty EditorText =
DependencyProperty.Register("ExpressionText", typeof(string), typeof(MyUerControl));

How can I access a view property from my ViewModel?

I have a ViewModel whose properties are bounded to from the View (XAML file).
I also have a property "StaticText" in the code behind file.
how can I access the property "StaticText" from inside the ViewModel ?
as suggested by Cameron, i've created a dependency property in my View :
String textToTest="I am just testing .";
public string TextToTest
{
get { return (string)this.GetValue(TextToTestProperty); }
set { this.SetValue(TextToTestProperty, value); }
}
public static readonly DependencyProperty TextToTestProperty =
DependencyProperty.Register("TextToTest", typeof(string),
typeof(MainWindow), new PropertyMetadata(false));
and I've added this to the constructor :
Binding aBinding = new Binding();
aBinding.Path = new PropertyPath("TextToTest");
aBinding.Source = viewModel;
aBinding.Mode = BindingMode.TwoWay;
this.SetBinding(TextToTestProperty, aBinding);
but I get an exception when I run the code.
By making the property a Dependency Property you can bind the property in the View to a property in the ViewModel.
public string TextToTest
{
get { return (string)this.GetValue(TextToTestProperty); }
set { this.SetValue(TextToTestProperty, value); }
}
public static readonly DependencyProperty TextToTestProperty =
DependencyProperty.Register("TextToTest", typeof(string),
typeof(MyControl), new PropertyMetadata(""));
See How to: Implement a Dependency Property

Is there a way to specify a custom dependency property's default binding mode and update trigger?

I would like to make it so that, as default, when I bind to one of my dependency properties the binding mode is two-way and update-trigger is property changed. Is there a way to do this?
Here is an example of one of my dependency properties:
public static readonly DependencyProperty BindableSelectionLengthProperty =
DependencyProperty.Register(
"BindableSelectionLength",
typeof(int),
typeof(ModdedTextBox),
new PropertyMetadata(OnBindableSelectionLengthChanged));
When registering the property, initialize your metadata with:
new FrameworkPropertyMetadata
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
}
In the Dependency Property declaration it would look like this:
public static readonly DependencyProperty IsExpandedProperty =
DependencyProperty.Register("IsExpanded", typeof(bool), typeof(Dock),
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnIsExpandedChanged));
public bool IsExpanded
{
get { return (bool)GetValue(IsExpandedProperty); }
set { SetValue(IsExpandedProperty, value); }
}

Categories

Resources