TextBox binding not working

I have two UserControls

:

1) ContactDetailsView

.

2) SearchBarView

.

In SearchBarView

I have a button Search

that invokes a command when clicked:Command="{Binding SearchCommand}"

In code SearchBarView

, I have the following code:DataContext = new SearchBarViewModel();

In ContactDetailsView

I have the following code:

xmlns:ViewModel="clr-namespace:Accounts_Manager.UserControls.SearchBar"

in the definition UserControl

and this code below it:

<UserControl.DataContext> <ViewModel:SearchBarViewModel /> </UserControl.DataContext>

I also have one TextBox

with the following definition:

<TextBox x:Name="ContactNameTextBox" FontFamily="Times New Roman" Foreground="DarkRed" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" IsEnabled="False" Grid.Column="0" Text="{Binding ContactId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

I have a class called SearchBarViewModel

with the following code:

    public string ContactId
    {
        get { return m_contactId; }
        set
        {
            m_contactId = value;
            OnPropertyChanged("ContactId");
        }
    }
    public ICommand SearchCommand
    {
        get { return m_searchCommand ?? (m_searchCommand = new RelayCommand(SearchContact)); }
        set { m_searchCommand = value; }
    }
    public void SearchContact(object parameter)
    {
        CurrentContact = DbHandler.Search("עידן");

        ContactId = CurrentContact.FirstName + " " + CurrentContact.LastName;
        BankId = CurrentContact.BankName;
        AccountNumber = CurrentContact.AccountNumber.ToString();
    }

      

The class is defined as follows; SearchBarViewModel : ViewModelBase

where ViewModelBase

inherits from INotifyPropertyChanged

and implements it.

RelayCommand

inherits from ICommand

and implements it.

And now, after all this, my problem is that when I click the button Search

while waiting for an update TextBox

, I see that everything is being called and updated, the property is hoisted, but TextBox

not updated with the value ContactId

.

Any ideas?

+3


source to share


1 answer


Simple, you are using two different objects!

DataContext = new SearchBarViewModel()

c SearchBarView

sets the DataContext where the button lives with the instance SearchBarViewModel

. No problems.

But then you do



<UserControl.DataContext>
    <ViewModel:SearchBarViewModel />
</UserControl.DataContext>

      

In another view, which also instantiates this object. Thus, calling the command changes the variable to its own viewmodel instance, which the other instance does not explicitly see.

User controls (like the search bar) usually don't have their own data context, you use dependency properties to let the host VM have properties associated with it. This is the approach I would take in your case. Another way to solve it would be to pass a shared instance to one or both controls so that they look at the same object.

+2


source







All Articles