繁体   English   中英

在UWP中使用Combobox SelectedItem和Binding

[英]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;
    }

页

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM