This is a migrated thread and some comments may be shown as answers.

Objects removed from NavigationProperty is not reflected in GUI

3 Answers 79 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Fredrik Lindros
Top achievements
Rank 1
Fredrik Lindros asked on 08 Oct 2013, 08:17 AM
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;
}

3 Answers, 1 is accepted

Sort by
0
Accepted
Kristian Nikolov
Telerik team
answered on 10 Oct 2013, 03:06 PM
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 >>
0
Fredrik Lindros
Top achievements
Rank 1
answered on 10 Oct 2013, 06:06 PM
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
0
Kristian Nikolov
Telerik team
answered on 11 Oct 2013, 09:26 AM
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 >>
Tags
General Discussions
Asked by
Fredrik Lindros
Top achievements
Rank 1
Answers by
Kristian Nikolov
Telerik team
Fredrik Lindros
Top achievements
Rank 1
Share this question
or