Converting a Normal Property to a Dependency Property

I have a control that I use for my new application. This control has the usual property as such.

Public Property Value() As String Get If AutoCompleteTextBox.SearchText Is Nothing Then Return String.Empty Else Return AutoCompleteTextBox.SearchText.ToString.Trim End If End Get Set(value As String) AutoCompleteTextBox.SearchText = value End Set End Property 

Edit:

So, after several attempts, I am finally at this point.

  Public Shared ValueProperty As DependencyProperty = DependencyProperty.Register("Value", GetType(String), GetType(AutoCompleteBox)) Public Property Value() As String Get Return Me.GetValue(ValueProperty).ToString End Get Set(value As String) Me.SetValue(ValueProperty, value) End Set End Property Public Event PropertyChanged As PropertyChangedEventHandler _ Implements INotifyPropertyChanged.PropertyChanged 

This is a dependency property. This property is still optional. Errors do not appear in the output window for binding.

 Text="{Binding RelativeSource={RelativeSource Self}, Path=Value, Mode=TwoWay}" 

This is my binding method. I have no idea what else I can do. At least if there was a mistake, I could come up with something. Without any mistake, I'm just a toothless chicken.

+5
source share
4 answers

Please refer to the following URL for all dependency basics http://www.wpftutorial.net/dependencyproperties.html

Basically, you can get the property of the changed property of the dependency property by providing FrameworkPropertyMetadata.

 new FrameworkPropertyMetadata( [Default Value], OnCurrentTimePropertyChanged); 

And you can return the target control (DependencyObject) to the event handler and implement your logic there

 private static void OnCurrentTimePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) { AutoCompleteTextBox control = source as AutoCompleteTextBox; string time = (string)e.NewValue; // Put some update logic here... } 
0
source

Declaring a dependency property in a control is good.

You can do some bindings in xaml (sorry, I don't have your XAML - I think).

Sort of:

 <TextBox x:Name="AutoCompleteTextBox" Text="{Binding RelativeSource={RelativeSource=Self},Path=Value}"/> 

Hello

0
source

TextBox has the Text property. When you access the Text property, it will give you the text entered in the TextBox. The same thing is in your case. Now, why do you want to convert it to DP? DP would be useful if you want to associate this DP with some other control.

Extend this control. Create a new control and introduce a new DP.

While using DP, where you want to bind this property to some control. This property is then updated using a control or control that is updated from this DP depending on the binding mode set.

How to bind:

  <TextBox x:Name="UserInput" /> <uc:MyAutoCompleteTextBox ValueDP="{Binding Text, ElementName=UserInput, Mode=OneWay}" /> 

MyAutoCompleteTextBox is a new control that extends (inherits) from your old AutoComplete control.

If you want to apply some filtering logic or something else, you can apply it in your DP like this:

 Get someVariable = TryCast(Me.GetValue(ValueProperty), String) ' apply somg logic to someVariable ' use your old Value property from here Return someVariable End Get 

There are many online tutorials for WPF.

I recommend: http://blog.scottlogic.com/2012/04/05/everything-you-wanted-to-know-about-databinding-in-wpf-silverlight-and-wp7-part-one.html

0
source

Just change your code to the following code and you should be good

your code

 Public Shared ReadOnly ValueProperty As DependencyProperty = DependencyProperty.Register("Value", GetType(String), GetType(AutoCompleteBox)) Public Property Value() As String Get Return TryCast(Me.GetValue(ValueProperty), String) End Get Set(value As String) Me.SetValue(ValueProperty, value) End Set End Property 

New code

 Public Shared ReadOnly ValueProperty As DependencyProperty = DependencyProperty.Register("Value", GetType(String), GetType(AutoCompleteBox)) Public Property Value() As String Get If AutoCompleteTextBox.SearchText Is Nothing Then Return String.Empty Else Return AutoCompleteTextBox.SearchText.ToString.Trim End If End Get Set(value As String) AutoCompleteTextBox.SearchText = value End Set End Property 

This DP will do what your Older property does. But just think about your requirement, what might be the best way to write things.

thanks

-1
source

Source: https://habr.com/ru/post/1233582/


All Articles