coderguy
coderguy

Reputation: 67

Databound textbox only updates after I edit it. Using MVVM in wpf form

My textbox is data bound to the selecteditem property of a Datagrid. So as user selects different row of the datagrid the textbox gets updated with the selected row's value. The data binding connections are set up correctly (almost). Reason I say is this. TextBox value is not updated until and unless it click on it and then hit any key. Here is a step by step what happens: 1. Say initial value of textbox is 1. 2. User clicks a row that has a column X value of 5. 3. I have to now click the textbox and type any key. Soon as I do that the selected row value appears. 4. Expected outcome should be no delay in user selecting a row and textbox's value updating.

So in essence the update of the textbox happens only after I click on it and make an edit. I have tried playing around with different UpdatetoSource property but nothing seems to work. Any help is appreciated. Also note this is my first ever question on StackOverflow so please forgive me if I am not following certain norms of asking questions (feedback welcome)

Model Class:
    class Account
      {
       public String AccountNumber{get;set;}
       public String CSSMeteterNumber{get;set;}
       public String CSSSDPNumber { get; set; }
      }

    ViewModel:
class AccountViewModel: INotifyPropertyChanged
    {

        public AccountViewModel()
        {
            Load(); 
        }

        private Account _account;
        public Account Account
        {
            get
            {
                return _account;
            }
            set
            {
                _account = value;
                OnPropertyChange(nameof(_account));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
       public void Load()
        {
            Account a = new Account { AccountNumber = "200", CSSMeteterNumber = "100", CSSSDPNumber = "100" };
            Account = a;
            ObservableCollection<Account> accounts = new ObservableCollection<Account>();
            for(int i=0; i<3;i++)
            {
                accounts.Add(new Account { AccountNumber = "200", CSSMeteterNumber = "100", CSSSDPNumber = "100" });
            }
            AccountCollection = accounts;
        }

public partial class AccountView : Window
    {
        public AccountView()
        {
            InitializeComponent();
            AccountViewModel aVM = new AccountViewModel();
            DataContext = aVM;
            CSS_E_DG.ItemsSource = aVM.AccountCollection.ToList();
        }
    }

AccountView Xaml
<Window x:Class="PlsWork.View.AccountView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PlsWork.View"
        mc:Ignorable="d"
        Title="AccountView" Height="450" Width="800">
    <StackPanel>

        <TextBox Text="{Binding Path=Account.AccountNumber, UpdateSourceTrigger=PropertyChanged}" Width="200" Height="40" Margin="0 0 0 10"/>

        <DataGrid x:Name="CSS_E_DG" AutoGenerateColumns="False" Width="300" Height="300"  SelectedItem="{Binding Path=Account}" >

        </DataGrid>
    </StackPanel>
</Window>

Upvotes: 0

Views: 100

Answers (2)

NetCoreDev
NetCoreDev

Reputation: 325

OnPropertyChange("Account")

or

OnPropertyChange("")

In this case, you will update all the properties

Upvotes: 2

coderguy
coderguy

Reputation: 67

OnPropertyChange(nameof(Account)); is the fix. Credit to @Çöđěxěŕ (see comments under post). Before I had OnPropertyChange(nameof(_account)); using the backing field as event arg.

Upvotes: 2

Related Questions