How to iterate through tabs and lists in them

I have a WPF application that is used to select tests that are later executed as part of an automated testing application.

In the UI, I have a few tab controls that represent each of the test categories. Within each tab there is a list of checkboxes for each test.

When the user selects all of their tests and hits the run button, I need to iterate through each of the tabs and lists in them to build the final list of tests. I don't want to have tons of expressions that follow this:

GetTab1Tests();
GetTab2Tests();
GetTab3Tests();
// Continue

      

I would like to create this in a way that I can say:

foreach(Tab t in tabControl)
{
    CollectTestsFromListbox(**listbox associated with t**);
}

      

How can I link this information? My best guess is to create a dictionary that links tabs to the required list, but is it good practice to move forward? As the application under test grows, the need for additional tabs may also grow.

EDIT For now, I tried to set the DataContext of the tabs:

<TabItem x:name="tabLoginTests DataContext="{Binding ElementName=lstboxLoginTests}">

      

And in the .xaml.cs file I can call:

tabLoginTests.DataContext

      

It all returns {System.Windows.Controls.ListBox Items.Count:4}

. I can't figure out how to take this object and grab the list control itself. Any suggestions?

+3


source to share


3 answers


The solution I was able to use is still valid in data context. First, I was unable to implicitly give an object in a list box, but eventually I realized that it was possible with as

.



var loginListBox = tabLoginTests.DataContext as ListBox

      

-1


source


Try adding the ListBox property to the Tag property for the intended TabControl when creating the ListBox.



Then you should be able to access it using t.Tag. Then you can pass it to ListBox (.Tag is an object).

0


source


You can use a simple Model-bound ItemsControl.

To access the selected tests, simply iterate over the lists using the Groups property.

public IEnumerable<Test> GetSelectedTests()
{
    return Groups.SelectMany(x => x.Tabs).SelectMany(x => x.Tests).Where(x => x.IsEnabled);
}

      

MainWindow.xaml

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="600" Width="640">
<Grid>
    <ItemsControl ItemsSource="{Binding Groups}" HorizontalAlignment="Center">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel></WrapPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name}" Margin="4"/>
                    <TabControl Width="300" MinHeight="250" Margin="4" ItemsSource="{Binding Tabs}">
                        <TabControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}"/>
                            </DataTemplate>
                        </TabControl.ItemTemplate>
                        <TabControl.ContentTemplate>
                            <DataTemplate>
                                <ListBox ItemsSource="{Binding Tests}">
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <CheckBox Content="{Binding Name}" IsChecked="{Binding IsEnabled}"/>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </DataTemplate>
                        </TabControl.ContentTemplate>
                    </TabControl>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

      

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<Group> Groups { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            Groups = new ObservableCollection<Group>();
            Groups.Add(new Group()
            {
                Name = "Group1",
                Tabs = new ObservableCollection<Tab>()
                {
                    new Tab()
                    {
                        Name = "Tab11",
                        Tests = new ObservableCollection<Test>()
                        {
                            new Test() { Name = "Test111", IsEnabled = true },
                            new Test() { Name = "Test112", IsEnabled = false }
                        }
                    },
                    new Tab()
                    {
                        Name = "Tab12",
                        Tests = new ObservableCollection<Test>()
                        {
                            new Test() { Name = "Test121", IsEnabled = true },
                            new Test() { Name = "Test122", IsEnabled = false },
                            new Test() { Name = "Test123", IsEnabled = false }
                        }
                    }
                }
            });

            Groups.Add(new Group()
            {
                Name = "Group2",
                Tabs = new ObservableCollection<Tab>()
                {
                    new Tab()
                    {
                        Name = "Tab21",
                        Tests = new ObservableCollection<Test>()
                        {
                            new Test() { Name = "Test211", IsEnabled = true },
                            new Test() { Name = "Test212", IsEnabled = false },
                            new Test() { Name = "Test213", IsEnabled = true }

                        }
                    },
                    new Tab()
                    {
                        Name = "Tab22",
                        Tests = new ObservableCollection<Test>()
                        {
                            new Test() { Name = "Test221", IsEnabled = true },
                            new Test() { Name = "Test222", IsEnabled = false },
                            new Test() { Name = "Test223", IsEnabled = false }
                        }
                    }
                }
            });

            DataContext = this;
        }
    }

    public class Group
    {
        public string Name { get; set; }
        public IList<Tab> Tabs { get; set; }
    }

    public class Tab
    {
        public string Name { get; set; }
        public IList<Test> Tests { get; set; }
    }

    public class Test
    {
        public string Name { get; set; }
        public bool IsEnabled { get; set; }
    }
}

      

0


source







All Articles