View bound to paged collection view not updating all of the time.

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2010-09-04T22:02:36Z Indexed on 2011/03/20 16:10 UTC
Read the original article Hit count: 264

Filed under:
|
|
|

I new to silverlight and trying to make a business application using the mvvm pattern and ria services. I have a view model class that contains a PagedCollectoinView and it is set to the item source of a datagrid. When I update the PagedCollectionView the datagrid is only updated the first time then after that subsequent changes to the data to not reflect in the view until after another edit. Things seem to be delayed one edit.

Below is a summarized example of my xaml and code behind.

This is the code for my view model

public class CustomerContactLinks : INotifyPropertyChanged
{
    private ObservableCollection<CustomerContactLink> _CustomerContact;
    public ObservableCollection<CustomerContactLink> CustomerContact
    {
        get
        {
            if (_CustomerContact == null)
                _CustomerContact = new ObservableCollection<CustomerContactLink>();
            return _CustomerContact;
        }
        set
        {
            _CustomerContact = value;
        }
    }

    private PagedCollectionView _CustomerContactPaged;
    public PagedCollectionView CustomerContactPaged
    {
        get
        {
            if (_CustomerContactPaged == null)
                _CustomerContactPaged = new PagedCollectionView(CustomerContact);
            return _CustomerContactPaged;
        }
    }

    private TicketSystemDataContext _ctx;
    public TicketSystemDataContext ctx
    {
        get
        {
            if (_ctx == null)
                _ctx = new TicketSystemDataContext();
            return _ctx;
        }
    }

    public void GetAll()
    {
        ctx.Load(ctx.GetCustomerContactInfoQuery(), LoadCustomerContactsComplete, null);
    }

    private void LoadCustomerContactsComplete(LoadOperation<CustomerContactLink> lo)
    {
        foreach (var entity in lo.Entities)
        {
            CustomerContact.Add(entity as CustomerContactLink);
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

Here is the basics of my XAML

     <Data:DataGrid x:Name="GridCustomers" MinHeight="100" MaxWidth="1000" IsReadOnly="True" AutoGenerateColumns="False">
                    <Data:DataGrid.Columns>
                        <Data:DataGridTextColumn Header="First Name" Binding="{Binding Customer.FirstName}" Width="105" />
                        <Data:DataGridTextColumn Header="MI" Binding="{Binding Customer.MiddleName}" Width="35" />
                        <Data:DataGridTextColumn Header="Last Name" Binding="{Binding Customer.LastName}" Width="105"/>
                        <Data:DataGridTextColumn Header="Address1" Binding="{Binding Contact.Address1}" Width="130"/>
                        <Data:DataGridTextColumn Header="Address2" Binding="{Binding Contact.Address2}" Width="130"/>
                        <Data:DataGridTextColumn Header="City" Binding="{Binding Contact.City}" Width="110"/>
                        <Data:DataGridTextColumn Header="State" Binding="{Binding Contact.State}" Width="50"/>
                        <Data:DataGridTextColumn Header="Zip" Binding="{Binding Contact.Zip}" Width="45"/>
                        <Data:DataGridTextColumn Header="Home" Binding="{Binding Contact.PhoneHome}" Width="85"/>
                        <Data:DataGridTextColumn Header="Cell" Binding="{Binding Contact.PhoneCell}" Width="85"/>
                        <Data:DataGridTextColumn Header="Email" Binding="{Binding Contact.Email}" Width="118"/>
                        </Data:DataGrid.Columns>
                </Data:DataGrid>

 <DataForm:DataForm x:Name="CustomerDetails" Header="Customer Details" AutoGenerateFields="False" AutoEdit="False" AutoCommit="False" 
                                  CommandButtonsVisibility="Edit"
                                  Width="1000" Margin="0,5,0,0">

                    <DataForm:DataForm.EditTemplate>

                    </DataForm:DataForm.EditTemplate>

                </DataForm:DataForm>

And here is my code behind

public Customers() { InitializeComponent(); BusyDialogIndicator.IsBusy = true; Loaded += new RoutedEventHandler(Customers_Loaded); CustomerDetails.BeginningEdit += new EventHandler(CustomerDetails_BeginningEdit); }

    void CustomerDetails_BeginningEdit(object sender, System.ComponentModel.CancelEventArgs e)
    {
        CustomerContacts.CustomerContactPaged.EditItem(CustomerDetails.CurrentItem);
    }

    private void Customers_Loaded(object sender, RoutedEventArgs e)
    {
        CustomerContacts = new CustomerContactLinks();
        CustomerContacts.GetAll();
        GridCustomers.ItemsSource = CustomerContacts.CustomerContactPaged;
        GridCustomerPager.Source = CustomerContacts.CustomerContactPaged;
        GridCustomers.SelectionChanged += new SelectionChangedEventHandler(GridCustomers_SelectionChanged);
        BusyDialogIndicator.IsBusy = false;
    }


    void GridCustomers_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        CustomerDetails.CurrentItem = GridCustomers.SelectedItem as CustomerContactLink;
    }


    private void SaveChanges_Click(object sender, RoutedEventArgs e)
    {
        if (WebContext.Current.User.IsAuthenticated)
        {
            bool commited = CustomerDetails.CommitEdit();
            if (commited && (!CustomerDetails.IsItemChanged && CustomerDetails.IsItemValid))
            {
                CustomerContacts.Update(CustomerDetails.CurrentItem as CustomerContactLink);
                CustomerContacts.ctx.SubmitChanges();
                CustomerContacts.CustomerContactPaged.CommitEdit();
                CustomerContacts.CustomerContactPaged.Refresh();       
                (GridCustomers.ItemsSource as PagedCollectionView).Refresh();
            }
        }
    }

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about gridview