gpt4 book ai didi

c# - UWP 中的组合框 SelectedItem 和绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 23:21:36 24 4
gpt4 key购买 nike

目前开发的 UWP 应用程序在使用组合框时遇到问题。

我正在将一个 ObservableCollection 绑定(bind)到一个组合框(它有效)

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;
}

page

关于c# - UWP 中的组合框 SelectedItem 和绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35229789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com