【发布时间】:2015-05-13 21:36:42
【问题描述】:
我在 HubSection 中有一个 ListBox,其项目绑定到通过后面的代码添加到我的 DefaulViewModel 的类“玩家”。 首先,我简单地将一个 TextBox 绑定到我的类“players”的属性“PlayerName”。 现在我想添加一个 ComboBox,其中包含一些不属于类播放器的项目。
有可能吗?我认为在 ComboBox 中定义 ItemsSource 会覆盖 ListBox 的 ItemsSource,但没有任何显示。
整个页面的DataContext定义如下:
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
那么HubSection就是这样的:
<HubSection x:Name="HubSec1">
<DataTemplate>
<ListBox x:Name="ListBox1" ItemsSource="{Binding players}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Path=PlayerName, Mode=TwoWay}"/>
<ComboBox ItemsSource="{Binding Path=ListOfElements}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</HubSection>
如果我以相同的方式定义组合框但在列表框之外,它将正确显示“ListOfElements”的字符串元素。 但是在这个 ListBox 中,ComboBox 是空的。所以我的猜测是,为 ListBox 定义了 ItemsSource,不可能覆盖它。
我试图定义一个 DataTemplate 但没有成功,但这可能是一个很好的解决方案(而且我没有正确进行)
我错过了什么?
编辑: ComboBox 项是一个 ObservableCollection。它不是“玩家”类的一部分。 这是我如何将这些元素添加到 DefaultViewModel
DefaultViewModel.Add("players", players);
DefaultViewModel.Add("MyItemsList", ListOfElements);
【问题讨论】: