I have a WPF MVVM app with two entities. One-to-many. I have two repositories, which provides me data for each entity from my database. They are storing in ef core local cache.
Here are my two collections in MainViewModel.
// MainViewModel
//Basically it's a link to local ef core cache in set repo
public ReadOnlyObservableCollection<Set> Sets { get; }
// DTO
public class Set(int id, string name) : IdDto(id)
{
public string Name { get; set; } = name;
public List<Word>? Words { get; set; }
}
// MainViewModel
//Also a link in word repo
public ReadOnlyObservableCollection<Word> Words { get; }
// DTO
public class Word(int id, string name, string? definition, string? imagePath, bool isFavorite, bool isLastWord, int setId) : IdDto(id)
{
public string Name { get; set; } = name;
public string? Definition { get; set; } = definition;
public string? ImagePath { get; set; } = imagePath;
public bool IsFavorite { get; set; } = isFavorite;
public bool IsLastWord { get; set; } = isLastWord;
public int SetId { get; set; } = setId;
public Set? Set { get; set; }
}
I have a collection of sets. Everyone of them contains a collection of words. So, how can i bind them in the view?
<ItemsControl ItemsSource="{Binding Sets}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type rep:Set}">
<!-- I was only be able to do something like this. -->
<TextBlock Text="{Binding Words.Count}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I have a button in this view. My ultimate goal is to press this button and go to another view with the selected set and it's words (bound entity).
Something like:
<Button CommandParametr={Binding SelectedSet, SelectedSet.Words} />
I have a WPF MVVM app with two entities. One-to-many. I have two repositories, which provides me data for each entity from my database. They are storing in ef core local cache.
Here are my two collections in MainViewModel.
// MainViewModel
//Basically it's a link to local ef core cache in set repo
public ReadOnlyObservableCollection<Set> Sets { get; }
// DTO
public class Set(int id, string name) : IdDto(id)
{
public string Name { get; set; } = name;
public List<Word>? Words { get; set; }
}
// MainViewModel
//Also a link in word repo
public ReadOnlyObservableCollection<Word> Words { get; }
// DTO
public class Word(int id, string name, string? definition, string? imagePath, bool isFavorite, bool isLastWord, int setId) : IdDto(id)
{
public string Name { get; set; } = name;
public string? Definition { get; set; } = definition;
public string? ImagePath { get; set; } = imagePath;
public bool IsFavorite { get; set; } = isFavorite;
public bool IsLastWord { get; set; } = isLastWord;
public int SetId { get; set; } = setId;
public Set? Set { get; set; }
}
I have a collection of sets. Everyone of them contains a collection of words. So, how can i bind them in the view?
<ItemsControl ItemsSource="{Binding Sets}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type rep:Set}">
<!-- I was only be able to do something like this. -->
<TextBlock Text="{Binding Words.Count}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I have a button in this view. My ultimate goal is to press this button and go to another view with the selected set and it's words (bound entity).
Something like:
<Button CommandParametr={Binding SelectedSet, SelectedSet.Words} />
Share
Improve this question
edited Feb 19 at 12:18
2033
asked Feb 11 at 12:12
20332033
18 bronze badges
4
|
1 Answer
Reset to default 0You'll have to nest an ItemsControl
with its ItemsSource
bound to to the Words
property of your Set
, something like
<ItemsControl ItemsSource="{Binding Sets}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:Set}">
<WrapPanel>
<Label Content="{Binding Name}" />
<!-- This ItemsControl is for the Words property of every Set -->
<ItemsControl ItemsSource="{Binding Words}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:Word}">
<Label Content="{Binding Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745212789a4616941.html
ItemsControl
bound to your individualSet
'sWords
property which in turn has its own template – MindSwipe Commented Feb 11 at 12:23