Objects removed from NavigationProperty is not reflected in GUI

4 posts, 1 answers
  1. Fredrik Lindros
    Fredrik Lindros avatar
    13 posts
    Member since:
    Aug 2012

    Posted 08 Oct 2013 Link to this post

    Hello

    In the example below i have simple DateTemplate used to display a customer and it's orders. Every OrderRow have Button with a DeleteCommand used to delete the order from the database. 

    I am having problems to get the ListBox with orders to reflect the changes made. If i execute the command the orderrow is deleted from the database but it is not reflected in the ListBox. 

    If i implement this with EntityFramework the changes are reflected in the GUI.

    How can i solve this issue?

    <Grid DataContext="{StaticResource vm}">
        <ListBox ItemsSource="{Binding VIEW_CUSTOMERS.View}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label Content="{Binding CustomerNumber}"/>
                        <Label Content="{Binding CustomerName}"/>
                        <ListBox ItemsSource="{Binding ORDERs}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Label Content="{Binding OrderNumber}"/>
                                        <Label Content="{Binding OrderDate}"/>
                                        <Button Content="Remove" Command="{Binding Cmd, Source={StaticResource vm}}" CommandParameter="{Binding}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>


    //////////////////////////////////
    //
    //Implementation of DeleteCommand
    //
    public ICommand Cmd { get; set; }
    private void Cmd_Execute(object parameter)
    {
        var order = parameter as ORDER;
     
        if (order != null)
        {
            order.CUSTOMER.ORDERs.Remove(order);
            this.Context.Delete(order);
            this.Context.SaveChanges();
        }
    }
    private bool Cmd_CanExecute(object parameter)
    {
        return true;
    }
  2. Answer
    Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 10 Oct 2013 Link to this post

    Hello Fredrik,

    The reason for the behavior you are experiencing is that your view does not know that the underlying properties have changed. To resolve the situation, you can either use TrackedBindingList for the navigation properties or implement the INotifyPropertyChanged interface in your view model.

    TrackedBindingList is a collection type which is integrated with the OpenAccess runtime and supports tracking of the addition and removal of its elements. If you use it for your collection navigation properties, removing or adding an element to them will cause the bound view to update accordingly. Depending on the language you are using and the type of your model, follow one of these tutorials to see how to change the type of collection navigation properties to TrackedBindingList:
    1. How to: Customize Collections (C# Domain Model)
    2. How to: Customize Collections (VB Domain Model)
    3. How to: Customize Collections (C# Fluent Model)
    4. How to: Customize Collections (VB Fluent Model)

    Another option is to implement INotifyPropertyChanged in your view model. Then, when you complete the deletion of an order, you can raise the PropertyChanged event for the property exposing the customers to your view. If in your case this is VIEW_CUSTOMERS, then the code would look as follows:

    public event PropertyChangedEventHandler PropertyChanged;
     
    public void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
     
    private void Cmd_Execute(object parameter)
    {
        var order = parameter as ORDER;
     
        if (order != null)
        {
            order.CUSTOMER.ORDERs.Remove(order);
            this.Context.Delete(order);
            this.Context.SaveChanges();
            OnPropertyChanged("VIEW_CUSTOMERS");
        }
    }

    If you are interested in seeing detailed examples of how to integrate Telerik OpenAccess ORM in your WPF or Silverlight MVVM application please check out our Samples Kit.

    I hope this helps.

    Regards,
    Kristian Nikolov
    Telerik
    OpenAccess ORM Q3 2013 Beta is available for immediate download in your account. Get it now and play with the latest bits. See what's new >>
  3. DevCraft banner
  4. Fredrik Lindros
    Fredrik Lindros avatar
    13 posts
    Member since:
    Aug 2012

    Posted 10 Oct 2013 Link to this post

    Hello

    Thank you for your reply.

    I tried raising the OnProperyChangedEvent and it works, i didn't expect it to refresh the listboxes on child level :-)

    I have a question regarding TrackedBindingList: 
    It would be nice if this could be an option when you generate the model from the database. Do you have any plans for this in the future? Today you have similar features like INotifyPropertyChanged, IDataErrorInfo etc


    Best regards Fredrik
  5. Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 11 Oct 2013 Link to this post

    Hi Fredrik,

    I am glad you have resolved your problem.

    As for the functionality to specify collection type when generating the model, we do have such feature in our backlog and will probably implement it in one of our future releases. Additionally, I have created a feature request in our feedback portal. Voting for it will help us prioritize this feature as compared to other requests.

    If you have more questions, feel free to use our ticket system or post in our forums again.

    Regards,
    Kristian Nikolov
    Telerik
    OpenAccess ORM Q3 2013 Beta is available for immediate download in your account. Get it now and play with the latest bits. See what's new >>
Back to Top