5

I have a control that I am using for my new application. This control has a regular 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 multiple tries, I am finally at this stage.

    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 the dependency property. This property is still not binding. No errors are shown in 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 an error, I could have figured out something. Without any error, I am just a headless chicken here.

jitendragarg
  • 945
  • 1
  • 14
  • 54

4 Answers4

0

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

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

new FrameworkPropertyMetadata( [Default Value], 
                   OnCurrentTimePropertyChanged);

And you can get back the target control (DependencyObject) at the event handler and implement your logic over there

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

Declaring a dependency property in a control is a good thing.

You could make some binding in the xaml (sorry I don't have your XAML - I imagine).

Something like :

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

Regards

Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
0

TextBox has a property called Text. When you access Text property it will give you text entered in TextBox. Same is your case. Now why you want to convert it into a DP ? A DP would be useful if you want o bind this DP to some other control.

Extend this control itself. Make a new control and introduce this new DP.

While a DP is used where you want to bind this property to some control. This property then gets updated from control or control gets updated from this DP depending upon binding mode set.

How to do binding :

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

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

If you want to apply some filtering logic or anything else, you can apply it in your DP itself 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 WPF Binding tutorials on net.

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

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • Ok, so the thing is, I have to bind this control to a datatable. Current code uses .Value in millions of places. So, I can't change that code. All I can do is create a new dependency property that updates this property. This way I can use the new dependency property in my code, while the old code will work as is. That's why I am trying to create new dependency property, let's call that ValueDP. Currently, .Value returns nothing. I have no idea why. And I cannot use ValueDP for binding either. – jitendragarg Oct 13 '15 at 07:54
  • Binding can be done only with DependencyObjects . The target of the object must be a dependency object. Source can be normal property, but source cannot target if source property is not a DP. http://blogs.msdn.com/b/wpfsdk/archive/2006/10/19/wpf-basic-data-binding-faq.aspx – AnjumSKhan Oct 13 '15 at 08:28
  • @jitendragarg i dont know how you are using this control. – AnjumSKhan Oct 13 '15 at 08:28
  • @jitendragarg updated my answer in response to your comment and updated question. You can also use WPF chat with me. – AnjumSKhan Oct 13 '15 at 08:43
  • Ok, I tried something similar. But the result is still not there. Anyway if you can join here, we can discuss the actual code I am writing. http://chat.stackoverflow.com/rooms/92117/wpf-dependency-property-binding – jitendragarg Oct 13 '15 at 09:04
-1

Just change your code with 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 was doing. But just think about your requirement there can be a better way of writing the things.

Thanks

Ankit Jain
  • 21
  • 1