How to bind related data many to many for example:

public class Post { public int PostId { get; set; } public string Title { get; set; } public ICollection<PostTag> PostTags { get; } = new List<PostTag>(); } public class Tag { public int TagId { get; set; } public string Text { get; set; } public ICollection<PostTag> PostTags { get; } = new List<PostTag>(); } public class PostTag { public int PostId { get; set; } public Post Post { get; set; } public int TagId { get; set; } public Tag Tag { get; set; } } 

Receiving:

 var Post_Tag = db.Posts.Include(a=>a.PostTags).ThenInclude(a=>a.Tag).ToList(); Items= new ObservableCollection<Abonoment>(); Post_Tag .ForEach(Items.Add); <ComboBox x:Name="Combo" DisplayMemberPath="Title" ItemsSource="{Binding Items}"/> <TextBlock Text="{Binding ElementName=Combo, Path=SelectedValue.PostTags.Tag.Text}" /> 

How to get Text from Tag?

  • First, read how to organize many-to-many communication in EF. You do not have to create a staging table manually, this should be done by the EF "behind the scenes". Secondly, in the selected Post the whole Tag collection, when you write SelectedValue.PostTags.Tag.Text , which Tag do you mean? The first? Last? Tenth? Seventh from the end? - Andrei NOP
  • @AndreyNOP, 1) I organized a connection on this guide blog.oneunicorn.com/2017/09/25/… 2) he should take exactly the one that is connected by id with Post`om - Sh.Djuraev
  • Well, let's go on the other side. PostTags is a List collection, does the collection have a Tag property? Post can be associated with a lot of Tag (and all by id). - Andrey NOP
  • @ AndreiNOP, yes, and what do you say by this? I take exactly the selected item by SelectedItem, do you want to say that by SelectedItem it will not take the data associated with it? - Sh.Djuraev
  • Consider the many-to-many relationship between a product and an order. Suppose we have Order No. 1 consisting of the goods Item 1, Item 2 and Item 3 and there is Order No. 2 consisting of the items Item 2, Item 3, Item 5. You display a list of orders, the user selected Order # 1 from the list. The question is, what product should appear in the SelectedValue. Products. Name? After all, there is more than one product in the order. And the Goods is a collection of goods, it does not have the property Name. To get started, try rewriting the binding like this: Path=SelectedValue.PostTags[0].Tag.Text - Andrey NOP

0