Data template for two counters that are grouped together.

Let's say I have the following two classes:

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; }
}

Some of the test data I have may look like this:

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

If I have one IEnumerable<TestResults>with IEnumerable<TestSummary>, I would like to be able to group them together on TestGroupand display their information something like this:

______________________________________________________________________
| Item1    _________________________  _______________________________ |
|          | Grid of TestResults   | | SummaryText for Item1        | |
|          |_______________________| |______________________________| |
|_____________________________________________________________________|
______________________________________________________________________
| Item2    _________________________  _______________________________ |
|          | Grid of TestResults   | | SummaryText for Item2        | |
|          |_______________________| |______________________________| |
|_____________________________________________________________________|

I have 2 questions on how to do this:

  • How can I join two IEnumerableand group them together so that they can be used with datatemplate?
  • Can my data template look like it displays grouped data?
+3
source share
1 answer

, ItemViewModel, TestResults SummaryText , . IEnumerable ItemViewModels ListView ( , ). , Grid of 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) 
    });

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 assume that your main VM contains property ViewModels 
     that is filled like described above -->
<ListView ItemsSource={Binding ViewModels} 
          ItemTemplate="{StaticResource ItemViewModelTemplate}" />
+4

Source: https://habr.com/ru/post/1772511/


All Articles