我在 HubSection 中有一个 ListBox,其 Items 绑定到通过后面的代码添加到我的 DefaulViewModel 中的“players”类。
首先,我只是将一个 TextBox 绑定到我的类“players”的属性“PlayerName”。
现在我想添加一个组合框,其中包含一些不属于类玩家的项目。
是否可以 ?我认为在 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);