C# 如何最好地显示此嵌套集合?(你能告诉我为什么我的代码不起作用吗?)

C# 如何最好地显示此嵌套集合?(你能告诉我为什么我的代码不起作用吗?),c#,wpf,xaml,mvvm,itemscontrol,C#,Wpf,Xaml,Mvvm,Itemscontrol,你能告诉我为什么这个代码不起作用吗 我有一个viewmodel,它有一个observablecollection的searchresults,它有一个observablecollection的resultproperties。我似乎无法像我所希望的那样显示结果属性的嵌套集合 以下是对象(为了可读性而抽象): 我得到的结果是 [Stack panel to keep things orderly] 1 2 3 PLACEHOLDER ///// 换句话说,绑定没有拾取字符串。我已经验证了集

你能告诉我为什么这个代码不起作用吗

我有一个viewmodel,它有一个observablecollection的searchresults,它有一个observablecollection的resultproperties。我似乎无法像我所希望的那样显示结果属性的嵌套集合

以下是对象(为了可读性而抽象):

我得到的结果是

[Stack panel to keep things orderly]
1

2

3

PLACEHOLDER /////
换句话说,绑定没有拾取字符串。我已经验证了集合是否像预期的那样填充。但是我不能让xaml工作

**添加信息

好的,我尝试了一些解决方案,但它们不起作用,所以我将添加更多细节,因为我可能缺少一些关于集合如何更新的信息

viewmodel上有一个名为“搜索”的按钮,该按钮使用IComand调用视图模型的TrySearch方法,该方法如下所示:

public void TrySearch()
{
    var results = _model.GetAll();
    foreach(var result in results)
        this.SearchResults.Add(new SearchResults<TDomain>(result));
}
public void TrySearch()
{
var results=_model.GetAll();
foreach(结果中的var结果)
this.SearchResults.Add(新的SearchResults(result));
}

这可能是因为集合的更新方式而无法工作的原因吗?SearchResults是一个依赖性属性(我知道我知道,它应该是INPC,但它不是,它的依赖性),但其他集合不是。这可能是个问题吗?

我将创建数据模板作为资源,并在XAML的其他地方引用它们。例如:

<Window ....>

    <Window.Resources>
        <DataTemplate x:Key="SearchResultTemplate" TargetType="SearchResults">
            <TextBlock Text={Binding PropertyValue}"
        </DataTemplate>

        <DataTemplate x:Key="ViewModelTemplate" TartgetType="ViewModel">
            <StackPanel>
                <TextBlock Text={Binding Result.Id}/>
                <StackPanel Orientation="Horizontal">
                    <ItemsControl ItemsSource={Binding ResultProperties} ItemTemplate="{StaticResource SearchResultTemplate}" />
                </StackPanel>
            </StackPanel
        </DataTemplate>

    </Window.Resources>

    <StackPanel>
        <ItemsControl ItemsSource={Binding SearchResults} ItemTemplate="{StaticResource ViewModelTemplate}" />
</Window>


您的代码在某种程度上是正确的,但根据您的需要,它有一个流程。StackPanel必须是ItemsPanel。更改如下:

[Stack panel to keep things orderly]
1
property1property2property3...
2
property1property2property3...
3
property1property2property3...
PLACEHOLDER /////
    <StackPanel>
        <ItemsControl ItemsSource="{Binding SearchResults}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Result.Id}"/>
                        <ItemsControl ItemsSource="{Binding ResultProperties}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" IsItemsHost="True"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding PropertyValue}" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <TextBlock Text="PLACEHOLDER /////"/>
    </StackPanel>


答案是我的SearchResults类没有正确连接。我使它成为一个具有依赖属性的依赖对象,并且工作正常。我假设如果它被inotifyproperty更改,也会有类似的翻译。感谢您的回复。

我几乎完全复制了您的代码(我已经删除了通用内容并生成了我自己的数据),而且它工作正常!到目前为止,所有的解决方案都没有奏效。我想知道是不是通用型导致了这个问题?但我需要通用的。我尝试了这个,它没有破坏任何东西,但我得到了和以前一样的结果。这给了我同样的结果。我将更新我原来的帖子,并尝试添加一些可能很重要的细节。
<Window ....>

    <Window.Resources>
        <DataTemplate x:Key="SearchResultTemplate" TargetType="SearchResults">
            <TextBlock Text={Binding PropertyValue}"
        </DataTemplate>

        <DataTemplate x:Key="ViewModelTemplate" TartgetType="ViewModel">
            <StackPanel>
                <TextBlock Text={Binding Result.Id}/>
                <StackPanel Orientation="Horizontal">
                    <ItemsControl ItemsSource={Binding ResultProperties} ItemTemplate="{StaticResource SearchResultTemplate}" />
                </StackPanel>
            </StackPanel
        </DataTemplate>

    </Window.Resources>

    <StackPanel>
        <ItemsControl ItemsSource={Binding SearchResults} ItemTemplate="{StaticResource ViewModelTemplate}" />
</Window>
    <StackPanel>
        <ItemsControl ItemsSource="{Binding SearchResults}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Result.Id}"/>
                        <ItemsControl ItemsSource="{Binding ResultProperties}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" IsItemsHost="True"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding PropertyValue}" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <TextBlock Text="PLACEHOLDER /////"/>
    </StackPanel>