MVVM binds the viewmodel property of a child to viewmodel property

Not sure if my headline explains the im problem well.

In my application, I make service calls -Confirm the list of clients. - create a list of organizations.

Then I will link this customer list to the list in my view.

In my view model, I have the following properties:

IEnumerable<Organisation> Organisations ObservableCollection<Customer> Customers 

Organization Properties: OrganizationId, OrganizationName

Customer Properties: CustomerId, OrganizationIdI, CustomerFirstName, CustomerLastName

Inside the Listbox in my view, I want to show the organization name for each client in the list.

How can I relate this to my point of view? I just want the text block to display the organization name for the client.

+4
source share
4 answers

I would smooth the model in the ViewModel client:

 class CustomerViewModel : INotifyPropertyChanged { public string OrgName { get; } public string FirstName {get; } public string LastName { get; } } 

Then the native ViewModel returns a collection of Clients:

 public class StoreViewModel : INotifyPropertyChanged { public ObservableCollection<CustomerViewModel> Customers { get; } } 

Bind the ListBox to the OrgName property in the CustomerViewModel:

 <ListBox> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding FirstName}"/> <TextBlock Text="{Binding OrgName}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 
+4
source

I would use MultiBinding and a MultiValueConverter for this, this, unfortunately, is not possible if you are limited to Silverlight, as tags offer, though ...

0
source

I agree with Ritch that you should flatten the model, but if you really don't want to do this, you can use IValueConverter. In the binding for where you want the name to be displayed, if you set the Organization as the datacontext of another control, you can perform the binding between the elements and pass another control to the datacontext in the binding, and in the ConverterParameter OrganizationId converter, then in the converter code use a bit of LINQ and return the name you want

0
source

Link the list to the Linq query opened from your ViewModel.

 Public IEnumerable ItemsView { get { return { from customer in this.Customers from org in this.Organisations where customer.OrganisationId=org.OrganisationId select new { FirstName=customer.FirstName, LastName=customer.LastName, OrganisationName=org.OrganisationName} }; } 

Then just bind the list in Ritch to this.

PS. I write this on my phone, so the code may not be perfect.

0
source

All Articles