Weirdmind
Weirdmind

Reputation: 19

Custom DependencyProperty of custom UserControl not binding properly

I have created a custom search user control.

public partial class HeaderSearchControl : UserControl
{
    public static DependencyProperty SearchTextProperty =
        DependencyProperty.Register("SearchText", typeof(string), typeof(HeaderSearchControl),
            new PropertyMetadata(new PropertyChangedCallback(SearchTextPropertyChanged)));

    public string SearchText
    {
        get { return (string)GetValue(SearchTextProperty); }
        set { SetValue(SearchTextProperty, value); }
    }

    public HeaderSearchControl()
    {
        InitializeComponent();
    }

    private static void SearchTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as HeaderSearchControl;
        if(control != null)
        {
            control.SearchBox.Text = (string)e.NewValue;
        }
    }
}

XAML looking like this:

<Border>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Border Grid.Column="0">
            <TextBox x:Name="SearchBox" Style="{StaticResource SearchTextBox}" Text="{Binding Path=SearchText}" />
        </Border>
        <StackPanel Orientation="Horizontal"  Grid.Column="1">
            <Button Style="{StaticResource IconSymbolButton}"  Content="&#xE721;" Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" Margin="0 2 2 2"/>
        </StackPanel>
    </Grid>
</Border>

The property in the view model looking like this:

private string _sessionFilter;
public string SessionFilter
{
    get { return _sessionFilter; }
    set
    {
        _sessionFilter = value;
    }
}

I am using it like this:

<local:HeaderSearchControl VerticalAlignment="Center" 
                           SearchText="{Binding SessionFilter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

When, for testing purposes, I change the value of the property in the view model on initialization, the correct value gets displayed and the program "reaches" the part, where the PropertyChangedCallback(SearchTextPropertyChanged) is defined.

However, when I am typing in the textbox, the property in the viewmodel doesn't seem to get updated. What am I missing? Can someone please help me?

Upvotes: 1

Views: 41

Answers (1)

ASh
ASh

Reputation: 35720

In current implementation SearchTextPropertyChanged removes Text binding and therefore there is no updates from HeaderSearchControl to bound property. SearchTextPropertyChanged has to go:

public partial class HeaderSearchControl : UserControl
{
    public static DependencyProperty SearchTextProperty = DependencyProperty.Register
    (
        "SearchText", 
         typeof(string), 
         typeof(HeaderSearchControl),
         new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
    );

    public string SearchText
    {
        get { return (string)GetValue(SearchTextProperty); }
        set { SetValue(SearchTextProperty, value); }
    }

    public HeaderSearchControl()
    {
        InitializeComponent();
    }
}

and Binding should use HeaderSearchControl as source:

<TextBox Style="{StaticResource SearchTextBox}" 
         Text="{Binding Path=SearchText, RelativeSource={RelativeSource AncestorType=UserControl}}" />

Upvotes: 2

Related Questions