鏈接兩個列表框之間的數據使用以下XAML引用從一個列表框中選擇的項目到其他listobx:
注:我綁定到使用一個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>
這裏是包括代碼以初始化集合視圖模型:
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; }
}
這裏查看更多e我初始化ViewModel:
public partial class MainView : Window
{
public MainView()
{
InitializeComponent();
this.DataContext = new ViewModels.MainViewModel();
}
}