jitendragarg
jitendragarg

Reputation: 955

Setter function not getting called on property binding change in WPF

I have a small piece of code in a user control. That piece of code registers a property as below

    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

Simple stuff, right. Nothing special till now. Inside this same control, I have a textbox with following binding

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

It is supposed to be very simple stuff. Sadly, this code is causing null exception, as Setter method is not getting called at all. I thought, I might be registering it wrong, or missing something. But, I have already gone through five different tutorials to create this property, and yet it is the same problem.

Edit:

Full XAML for the control. I want to update the value property in this control based on the searchtext property change. I tried replacing that autocomplete with normal textbox too, same issue.

<UserControl x:Class="AutoCompleteBox"
         xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" xmlns:UC="clr-namespace:SMS" >
<StackPanel Margin="0">
    <StackPanel Orientation="Horizontal" FlowDirection="LeftToRight">
        <Label Content="" Width="auto" x:Name="AutocompleteLabel" Style="{DynamicResource LabelStyle}" Visibility="Collapsed"/>
        <Label Content="*" Width="auto" x:Name="MandatoryLabel" Style="{DynamicResource LabelStyle}" FontSize="22" Foreground="Red" Visibility="Collapsed"/>
    </StackPanel>
    <telerik:RadAutoCompleteBox x:Name="AutoCompleteTextBox" SearchText="{Binding RelativeSource={RelativeSource Self}, Path=Value, Mode=TwoWay}" KeyboardNavigation.TabNavigation="Local" TextSearchMode="Contains" AutoCompleteMode="Suggest" Height="30" Margin="0,0,5,0" GotFocus="AutoCompleteTextBox_GotFocus" KeyUp="AutoCompleteTextBox_KeyUp" telerik:StyleManager.Theme="Windows8" SelectionMode="Single"></telerik:RadAutoCompleteBox>
</StackPanel>

Edit:

Screenshot of the issue. https://i.sstatic.net/CAUlo.jpg

Upvotes: 0

Views: 80

Answers (1)

King King
King King

Reputation: 63347

The DependencyProperty you created has a default value of Nothing (null in C#). So when you call GetValue(ValueProperty).ToString then of course a NullReferenceException will throw. What you should do is using the TryCast to cast it to String in a safe manner:

Return TryCast(Me.GetValue(ValueProperty), String)

Edit: The Binding in your code should surely fail, the Value property is defined in AutoCompleteBox so the Binding should look up the visual tree for AutoCompleteBox (which is the UserControl) like this:

SearchText="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, 
             Path=Value, Mode=TwoWay}"

Upvotes: 1

Related Questions