情况就是这样:
我有一系列藏品!
我有2个列表框!
ListBox A包含我的集合集合,因为它是itemsource并支持多项选择(SelectionMode = Extended)
ListBox B需要从ListBox A中选择的所有集合的合成中获取它的itemsource。
有没有好办法呢?
数据结构如下
TestContainers []。TestEntries []
如果测试容器A&选择C然后列表框B包含容器A和容器A中的所有TestEntries。 ç
我希望这很清楚?
答案 0 :(得分:1)
要链接两个列表框之间的数据,请使用以下XAML将所选项目从一个列表框引用到另一个列表框:
注意:我使用ViewModel绑定到ObservableCollection;我在下面提供了大部分代码,以便在需要时重新构建此代码。
<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>
以下是ViewModel,其中包含初始化集合的代码:
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;
}
}
}
这是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;
}
}
}
这是TestEntry:
public class TestEntry
{
public string EntryName { get; set; }
}
这里是我初始化ViewModel的视图:
public partial class MainView : Window
{
public MainView()
{
InitializeComponent();
this.DataContext = new ViewModels.MainViewModel();
}
}
答案 1 :(得分:0)
CompositeCollection班怎么样?