1
votes

In one of my Data Models I have a string property that is bound to the text of a textBox. When I change the text of this textBox I would like to update a property in another ViewModel. To achieve this, I believe that I would have to perform the operations under the property's setter, but whenever I change the textbox's text the only area of the string property that is called is the getter. There must be something that I don't know or am overlooking..

This is the string property that I am working with just in case it helps to see it:

public string DisplayName
{
    get { return _displayName; }
    set
    {
        _displayName = value;
        NotifyPropertyChange(() => DisplayName);

        //These values are from the other ViewModel
        if (MainTreeCollection.SelectedItem != null
               && value != MainTreeCollection.SelectedItem.DisplayName)
             MainTreeCollection.SelectedItem.DisplayName = value;
    }
}

I noticed in this question he shows how to "get [his] ViewModel to know when the user has changed text in the TextBox and moved the focus away from the TextBox". It looks like he has achieved this by binding to a string property, which is what I did above.

How come the property's setter is not getting accessed? How would I change my method and code to perform these operations?

Update: This is the xaml of the textBox as requested:

<TextBox Text="{Binding Model.DisplayName}" Height="23"
         HorizontalAlignment="Left" Name="title_TB"
         VerticalAlignment="Top" Width="Auto" FontWeight="Bold"
         FontSize="14" Margin="5,2,0,0" />
1
Can you show the "setting" code (the actual assignment), or is this coming from the UI? - BradleyDotNET
This is the code in the DataModel. You want the xaml of the textBox? - Eric after dark
Can you show the definition of the binding? - phoog
@EricAfterDark so you're actually using DataBinding after all.. who would have thought.... - Federico Berasategui
@HighCore That is hilarious that I was just thinking about you. Of course.. I always use data binding! - Eric after dark

1 Answers

1
votes

In case you want bounded property to be set while you are typing into it, set UpdateSourceTrigger to PropertyChanged.

Default value is LostFocus for TextBox i.e. bounded property gets updated only when LostFocus event gets raised for TextBox.

<TextBox Text="{Binding Model.DisplayName,
                        UpdateSourceTrigger=PropertyChanged}"
         Height="23" HorizontalAlignment="Left" Name="title_TB"
         VerticalAlignment="Top" Width="Auto" FontWeight="Bold"
         FontSize="14" Margin="5,2,0,0"/>