WPF custom UserControl - Property Binding
WPF custom UserControl - Property Binding
I try to develop a UserControl
look like a TextBox
white two different changes.
UserControl
TextBox
First of all the new TextBox
has to display a "PlaceholderText" if the TextBox
text value is empty. My solution for this implementation includes a second TextBox
white the "PlaceholderText" as simply Text Attribute. At last I changed the visibility an the focus to the other TextBox
.
TextBox
TextBox
TextBox
TextBox
An when the Textbox ValidationResult Object return false they display a TextBlock
white an "ErrorMessage"
TextBlock
They tow implementations are already working and existent. For my new TextBox
I copied all the TextBox
specific properties into my new control and passed them to the original TextBox
.
TextBox
TextBox
TextBox
Now I tried to bind the Text
property from my new control to a DependencyPropery Object (in the ViewModel).
Text
My implementation looks this:
Custom TextBox Text property
public string Text
{
get => TbSource.Text;
set => TbSource.Text = value;
}
ViewModel propdp
public static DependencyProperty PersonProperty =
DependencyProperty.Register(nameof(Person), typeof(Person), typeof(PersonViewModel));
public Person Person
{
get => (Person)GetValue(PersonProperty);
set => SetValue(PersonProperty, value);
}
And my view
<customControl:NiceTextBox Grid.Row="0" Grid.Column="1" IsPlaceholderAktive="True" PlaceholderText="Enter first name" ErrorMessage="The given first name isn't valid." Text="{Binding Person.Name}" />
Now in the implementation in the View I became follow message:
Has anyone an idea how to fix it? I tried to change my Text property to a dependency property but then I can't pass the input and output from the TbSource.
2 Answers
2
The Text
property of your custom control - the target property - must be a dependency property for you to be able to bind to it like this in XAML:
Text
<customControl:NiceTextBox ... Text="{Binding Person.Name}" />
But the Person
property in the view model - the source property - shouldn't be defined as a dependency property.
Person
So you have defined the dependency property in the wrong class. Only target properties must be defined as dependency property for you to be able to bind them to some source property.
A control inherits from a DependencyObject
class where the GetValue
and SetValue
methods are defined but a view model generally doesn't.
DependencyObject
GetValue
SetValue
Make your UserControl Text
property as DependencyProperty
and the property in ViewModel
as a normal CLR property and bind it.
Text
DependencyProperty
ViewModel
UserControl
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(NiceTextBox), new PropertyMetadata(string.Empty));
ViewModel
public Person Person { get; set; }
public Person Person { get; set; }
XAML
<customControl:NiceTextBox ... Text="{Binding Person.Name}" />
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment