简体   繁体   English

在列表上单击时,请选择ComboboxItem

[英]Uwp Select ComboboxItem when click on list

I have a form with a 2 items combobox in XAML: 我在XAML中有一个包含2个项目组合框的表单:

<ComboBox x:Name="cb_Category" PlaceholderText="Category" HorizontalAlignment="Left" Height="40" Margin="20,88,0,0" VerticalAlignment="Top" Width="437" SelectionChanged="cb_Categoria_SelectionChanged">
<ComboBoxItem Content="Products"/>
<ComboBoxItem Content="Services"/>
</ComboBox>

I converted the combobox selected item to string so it can be added to my database and appear on my list. 我将组合框选择的项目转换为字符串,因此可以将其添加到数据库中并显示在列表中。

    private void cb_Category_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (cb_Category.SelectedItem != null)
        {
            var combo = (ComboBox)sender;
            var item = (ComboBoxItem)combo.SelectedItem;
            content = item.Content.ToString();
        }
    }

Now what I want to do is when I select a item in my list, the combobox selects and show the same item. 现在,我要做的就是在列表中选择一个项目时,组合框会选择并显示相同的项目。 But I have no idea how? 但是我不知道怎么办?

App Sample 应用范例

As you can see when I click on a Grid Item I get all the values of the item in the textboxes except the combobox 如您所见,当我单击“网格项目”时,我在文本框中获得了该项目的所有值(组合框除外)

In order to set the List item to your ComboBox you need to set the ItemsSource property as below: 为了将List项设置为ComboBox,需要设置ItemsSource属性,如下所示:

cb_Category.ItemsSource = yourList;

EDIT as per the updated Post 根据更新后的帖子进行编辑

Once your data is loaded you can do something like this on list SelectionChanged : 加载数据后,可以在列表SelectionChanged

cb_Category.SelectedIndex = cb_Category.Items.IndexOf(myListView.SelectedItem);

If your List and ComboBox is not sharing the same data structure then you can do something like this: 如果您的List和ComboBox没有共享相同的数据结构,则可以执行以下操作:

Your List data structure: 您的列表数据结构:

Public Class MyList
{
  public int Property1 {get;set;}
  public int Property2 {get;set;}
  public string Property3 {get;set;} //Property mapped for ComboBox
  public int Property4 {get;set;}
}

Your ComoboBox DataStructure: 您的ComoboBox数据结构:

Public Class MyComboBox
{
  public int Property1 {get;set;}
  public string Property2 {get;set;} //Property that needs to be display in ComboBox
}

Than on list SelectionChanged first find the item in your ComboBox and then set it's `SelectedIndex' 比在列表SelectionChanged先在ComboBox找到该项目,然后将其设置为“ SelectedIndex”

var selectedItem = myComboBoxDataSource.where(x=>x.Property2.Equals(((MyList)myListView.SelectedItem).Property3));
cb_Category.SelectedIndex = cb_Category.Items.IndexOf(selectedItem);

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

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