1

I'm trying to work with a datagrid using the MVVM pattern. The problem is when I select an item in datagrid for the first time, the datagrid will update the source property correctly, then I select another item, the source property doesn't update. This is my binding in xaml:

<DataGrid ItemsSource="{Binding Customers}" 
          SelectedItem="{Binding SelectedCustomer, Mode=TwoWay, 
              UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">

ViewModel code:

public Customer SelectedCustomer
{
    get { return _selectedCustomer; }
    set
    {
        if (value != _selectedCustomer)
        {
            _selectedCustomer = value;
            NotifyOfPropertyChange(() => SelectedCustomer);
        }
    }
}

Thanks in advance!

4
  • are you sure that Customer you are setting in SelectedCustomer is one of Customers children. notice that the SelectedCustomer must be the same Reference of one of Customers children not the same customer. (the same Reference not customer) it is very imprtant. Commented Apr 12, 2013 at 16:10
  • @MoHaKa Thanks for your comment.I'm sure they are reference the same customer. Commented Apr 12, 2013 at 16:29
  • Do you get any binding errors in the output window after the first selection? And just to be clear you're saying that the SelectedCustomer doesn't update after the first selection, correct? It looks like you might be using CaliburnMicro have you tried just setting the x:Name="Customers" and seeing if CM can handle the binding for you? Commented Apr 12, 2013 at 21:26
  • @JasonMassey No binding errors.Yes,the SelectedCustomer doesn't update after the first selection.The binding is not Handled by CM, actually the datagrid bound to a list of custom objects. Commented Apr 13, 2013 at 2:18

2 Answers 2

2

I have solved the problem, the reason that there is a spelling mistake on Customer's Equals method.

Sign up to request clarification or add additional context in comments.

Comments

1

It appears you implementation may be slightly off. Generally you implement the ModelView to implement 'INotifyPropertyChanged' of a change after a property has changed. You bind to an observable collection of which generally you have a list of members similar to what you have but I generally do it like:

public string Email
        {
            get { return _customer.Email; }
            set
            {
                if (value == _customer.Email)
                    return;

                _customer.Email = value;

                OnPropertyChanged("Email");  // Text should match property name
            }
        }

In this example _customer would be a private member of a POCO object that has it's own properties set up. You generally bind to the parent collection as the datasource and then implement properties on it's members. I got this from the main MVVM MSDN example for reference too: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

I also am not certain but once I generally bind the data Itemssource={Binding (collectionName in ModelView)} I just list out the text of the memebers of that collection. EG: selectitem = "email" which is a property of the collection. I also generally use the 'observable collection' data type as this will implement the changes. I am not certain if lists and other collections are used as much in MVVM for this reason. I know 'readonlycollection' and 'observablecollection' are the most common.

1 Comment

Thanks.In my scenario,the SelectedItem is the Customer object not its property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.