Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 分组在一起的两个枚举的数据模板_C#_.net_Silverlight_Linq_Xaml - Fatal编程技术网

C# 分组在一起的两个枚举的数据模板

C# 分组在一起的两个枚举的数据模板,c#,.net,silverlight,linq,xaml,C#,.net,Silverlight,Linq,Xaml,假设我有以下两个类: public class TestResults { public string TestGroup {get; set; } public string TestName {get; set; } public bool TestPassed {get; set; } } public class TestSummary { public string TestGroup {get; set; } public string Su

假设我有以下两个类:

public class TestResults
{
    public string TestGroup {get; set; }
    public string TestName {get; set; }
    public bool TestPassed {get; set; }
}

public class TestSummary
{
    public string TestGroup {get; set; }
    public string SummaryText {get; set; }
}
我拥有的一些测试数据可能如下所示:

TestResults
TestGroup       TestName        TestPassed
==========================================
Item1           HasPrice        True
Item1           HasDiscount     True
Item2           HasPrice        True
Item2           HasDiscount     False

TestSummary
TestGroup    SummaryText
=================================================
Item1        SKU: 234 Price: 13.40 Discount: 1.34
Item2        SKU: 345 Pirce: 15.70
______________________________________________________________________
| Item1    _________________________  _______________________________ |
|          | Grid of TestResults   | | SummaryText for Item1        | |
|          |_______________________| |______________________________| |
|_____________________________________________________________________|
______________________________________________________________________
| Item2    _________________________  _______________________________ |
|          | Grid of TestResults   | | SummaryText for Item2        | |
|          |_______________________| |______________________________| |
|_____________________________________________________________________|
如果我有一个
IEnumerable
和一个
IEnumerable
,我希望能够在
TestGroup
上将它们组合在一起,并显示它们的信息如下:

TestResults
TestGroup       TestName        TestPassed
==========================================
Item1           HasPrice        True
Item1           HasDiscount     True
Item2           HasPrice        True
Item2           HasDiscount     False

TestSummary
TestGroup    SummaryText
=================================================
Item1        SKU: 234 Price: 13.40 Discount: 1.34
Item2        SKU: 345 Pirce: 15.70
______________________________________________________________________
| Item1    _________________________  _______________________________ |
|          | Grid of TestResults   | | SummaryText for Item1        | |
|          |_______________________| |______________________________| |
|_____________________________________________________________________|
______________________________________________________________________
| Item2    _________________________  _______________________________ |
|          | Grid of TestResults   | | SummaryText for Item2        | |
|          |_______________________| |______________________________| |
|_____________________________________________________________________|
关于如何做到这一点,我有两个问题:

  • 如何将这两个
    IEnumerable
    s连接起来并将它们分组,以便数据模板可以使用它们
  • 如果我的数据模板能够显示分组的数据,它会是什么样子

  • 我认为您需要创建一个新的类ItemViewModel,它将包含相应的TestResults和SummaryText作为您可以绑定到的属性。这样,您就可以创建ItemViewModels的IEnumerable并绑定ListView(或任何您想要的)。我还建议用绑定到视图模型的TestResults属性的ListView替换TestResults网格

    希望这有帮助!简要描述-如果我的解释不够清楚,请留下评论

    UPD:

    此代码应填充视图模型的IEnumerable:

    public class ItemViewModel
    {
        public IEnumerable<string> TestResults { get; set; }
        public string SummaryText { get; set; }
    }
    
    ...
    
    var viewModels = testResults.Select(tr => 
        new ItemViewModel() { 
          TestResults = testResults.Where(t => t.TestGroup == tr.TestGroup)
                                   .Select(t => t.TestName + " " + t.TestPassed),
          SummaryText = testSummaries.First(ts => ts.TestGroup == tr.TestGroup) 
        });
    
    公共类ItemViewModel
    {
    公共IEnumerable测试结果{get;set;}
    公共字符串摘要文本{get;set;}
    }
    ...
    var viewModels=testResults.Select(tr=>
    新建ItemViewModel(){
    TestResults=TestResults.Where(t=>t.TestGroup==tr.TestGroup)
    .选择(t=>t.TestName+“”+t.TestPassed),
    SummaryText=testSummaries.First(ts=>ts.TestGroup==tr.TestGroup)
    });
    
    UPD2:

    这个XAML应该做到以下几点:

    <!-- In your resources -->
    <DataTemplate x:Key="ItemViewModelTemplate">
        <StackPanel Orientation="Horizontal">
            <ListView ItemsSource={Binding TestResults} />
            <TextBlock Text={Binding SummaryText} />
        </StackPanel>
    </DataTemplate>
    
    ...
    
    <!-- In your main xaml file -->
    <!-- Let's assume that your main VM contains property ViewModels 
         that is filled like described above -->
    <ListView ItemsSource={Binding ViewModels} 
              ItemTemplate="{StaticResource ItemViewModelTemplate}" />
    
    
    ...
    
    谢谢你的建议,但我不确定该如何实施。好的,谢谢。我一直在思考如何将数据组合在一起。知道如何让它显示在数据模板中吗?如果视图模型的两个属性都是primitivies,这将很简单,但由于其中一个是可枚举的,我不确定从哪里开始。