0
votes

So the situation is this:

I have a collection of collections!

I have 2 list boxes!

ListBox A contains my collection of collections as it's itemsource and supports multiple selection (SelectionMode = Extended)

ListBox B needs to get it's itemsource from a composite of all collections selected in ListBox A.

Is there a good way to do this?

data structure is as follows

TestContainers[].TestEntries[]

if test Container A & C are selected then listbox B contains all of the TestEntries in both Container A & C

I hope this is clear?

2

2 Answers

1
votes

To link the data between two listboxes reference the selected items from one list box to the other listobx using the following XAML:

Note: I am binding to an ObservableCollection using a ViewModel; I included most of my code below to allow rebuilding this code if needed.

<Window x:Class="TwoListBoxesSameData.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Main Window" Height="400" Width="800">
  <Window.Resources>
    <DataTemplate x:Key="ListBoxTemplate">
        <TextBlock>
           <TextBlock Text="{Binding Path=ContainerName}" />
        </TextBlock>
    </DataTemplate>
    <DataTemplate x:Key="ListBoxTemplate2" >
        <TextBlock>
           <TextBlock Text="{Binding Path=TestEntries[0].EntryName}" />
        </TextBlock>
    </DataTemplate>
  </Window.Resources>
  <DockPanel>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ListBox Grid.Row="0" 
                 x:Name="lb"
                 ItemsSource="{Binding Path=TestContainers}"
                 ItemTemplate="{Binding Source={StaticResource ListBoxTemplate}}"
                 SelectionMode="Extended">
        </ListBox>
        <ListBox Grid.Row="1" 
                 ItemsSource="{Binding ElementName=lb, Path=SelectedItems}"
                 ItemTemplate="{Binding Source={StaticResource ListBoxTemplate2}}" >
        </ListBox>
    </Grid>
  </DockPanel>
</Window>

Here is the ViewModel that includes the code to initialise the collections:

public class MainViewModel : ViewModelBase
{
  public MainViewModel()
  {
     {
        TestContainer tc1 = new TestContainer();
        tc1.ContainerName = "Container 1";

        TestEntry te1 = new TestEntry();
        te1.EntryName = "Search for Names";
        tc1.TestEntries.Add(te1);

        TestEntry te2 = new TestEntry();
        te2.EntryName = "Search for People";

        tc1.TestEntries.Add(te2);
        TestEntry te3 = new TestEntry();
        te3.EntryName = "Search for Things";
        tc1.TestEntries.Add(te3);

        _testContainers.Add(tc1);
     }
     {
        TestContainer tc2 = new TestContainer();
        tc2.ContainerName = "Container 2";

        TestEntry te1 = new TestEntry();
        te1.EntryName = "Look for Names";
        tc2.TestEntries.Add(te1);

        TestEntry te2 = new TestEntry();
        te2.EntryName = "Look for People";
        tc2.TestEntries.Add(te2);

        TestEntry te3 = new TestEntry();
        te3.EntryName = "Look for Things";
        tc2.TestEntries.Add(te3);

        _testContainers.Add(tc2);
     }
     {
        TestContainer tc3 = new TestContainer();
        tc3.ContainerName = "Container 3";

        TestEntry te1 = new TestEntry();
        te1.EntryName = "Find Names";
        tc3.TestEntries.Add(te1);

        TestEntry te2 = new TestEntry();
        te2.EntryName = "Find People";
        tc3.TestEntries.Add(te2);

        TestEntry te3 = new TestEntry();
        te3.EntryName = "Fine Things";
        tc3.TestEntries.Add(te3);

        _testContainers.Add(tc3);
     }
  }

  private ObservableCollection<TestContainer> _testContainers = new ObservableCollection<TestContainer>();
  public ObservableCollection<TestContainer> TestContainers
  {
     get
     {
        return _testContainers;
     }
     set
     {
        _testContainers = value;
     }
  }
}

Here is TestContainer:

public class TestContainer
{
  public string ContainerName { get; set; }

  private ObservableCollection<TestEntry> _testEntries = new ObservableCollection<TestEntry>();
  public ObservableCollection<TestEntry> TestEntries
  {
     get
     {
        return _testEntries;
     }
     set
     {
        _testEntries = value;
     }
  }
}

Here is TestEntry:

public class TestEntry
{
   public string EntryName { get; set; }
}

Here the View where I initialise the ViewModel:

public partial class MainView : Window
{
  public MainView()
  {
     InitializeComponent();

     this.DataContext = new ViewModels.MainViewModel();
  }
}
0
votes