最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How should I bind two bound entities in the view? - Stack Overflow

programmeradmin0浏览0评论

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 You'll have to nest another ItemsControl bound to your individual Set's Words property which in turn has its own template – MindSwipe Commented Feb 11 at 12:23
  • You need to use a JOIN query. – jdweng Commented Feb 11 at 13:11
  • @jdweng You mean "Include"? Yeah, i should, but i don't know how to deal with it later. I mean how to add or delete data. – 2033 Commented Feb 11 at 14:08
  • see stackoverflow/questions/21051612/… – jdweng Commented Feb 11 at 17:53
Add a comment  | 

1 Answer 1

Reset to default 0

You'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>
发布评论

评论列表(0)

  1. 暂无评论