UWP 中的组合框 SelectedItem 和绑定
Combobox SelectedItem and Binding in UWP
目前开发的 UWP 应用程序在使用组合框时遇到问题。
我正在将 ObservableCollection 绑定到组合框(有效)
var WarehouseList = new ObservableCollection<Warehouse>(taskmag.Result);
WarehouseBox.ItemsSource = WarehouseList;
我想做的是在我将数据加载到我的表单时显示一个选定的项目。
我没有使用 MVVM,这是我的 Combox XAML
<ComboBox HorizontalAlignment="Stretch" Width="400" FontSize="32" Name="WarehouseBox" Margin="20,0,0,0">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="Auto" Height="Auto">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding WarehouseID}" Name="MID" Visibility="Collapsed"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我不知道从哪里开始,因为文档总是暗示 MVVM 或其他一些我没有实现的东西。
如果可以帮助解决问题,我愿意将我的项目 coll 更改为 List 或 IEnumerable。
非常感谢任何帮助。
这是全部内容,如果它仍然不适合您,请告诉我:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ComboBox x:Name="ComboBoxWarehouses">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="Auto" Height="Auto">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
public MainPage()
{
this.InitializeComponent();
var items = new ObservableCollection<Warehouse>();
var item = new Warehouse() {Name = "selected"};
items.Add(new Warehouse() { Name = "not selected"});
items.Add(item);
items.Add(new Warehouse() { Name = "Another Not Selected"});
ComboBoxWarehouses.ItemsSource = items;
ComboBoxWarehouses.SelectedItem = item;
}
目前开发的 UWP 应用程序在使用组合框时遇到问题。
我正在将 ObservableCollection 绑定到组合框(有效)
var WarehouseList = new ObservableCollection<Warehouse>(taskmag.Result);
WarehouseBox.ItemsSource = WarehouseList;
我想做的是在我将数据加载到我的表单时显示一个选定的项目。 我没有使用 MVVM,这是我的 Combox XAML
<ComboBox HorizontalAlignment="Stretch" Width="400" FontSize="32" Name="WarehouseBox" Margin="20,0,0,0">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="Auto" Height="Auto">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding WarehouseID}" Name="MID" Visibility="Collapsed"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我不知道从哪里开始,因为文档总是暗示 MVVM 或其他一些我没有实现的东西。 如果可以帮助解决问题,我愿意将我的项目 coll 更改为 List 或 IEnumerable。
非常感谢任何帮助。
这是全部内容,如果它仍然不适合您,请告诉我:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ComboBox x:Name="ComboBoxWarehouses">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="Auto" Height="Auto">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
public MainPage()
{
this.InitializeComponent();
var items = new ObservableCollection<Warehouse>();
var item = new Warehouse() {Name = "selected"};
items.Add(new Warehouse() { Name = "not selected"});
items.Add(item);
items.Add(new Warehouse() { Name = "Another Not Selected"});
ComboBoxWarehouses.ItemsSource = items;
ComboBoxWarehouses.SelectedItem = item;
}