繁体   English   中英

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

[英]Uwp Select ComboboxItem when click on list

我在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>

我将组合框选择的项目转换为字符串,因此可以将其添加到数据库中并显示在列表中。

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

现在,我要做的就是在列表中选择一个项目时,组合框会选择并显示相同的项目。 但是我不知道怎么办?

应用范例

如您所见,当我单击“网格项目”时,我在文本框中获得了该项目的所有值(组合框除外)

为了将List项设置为ComboBox,需要设置ItemsSource属性,如下所示:

cb_Category.ItemsSource = yourList;

根据更新后的帖子进行编辑

加载数据后,可以在列表SelectionChanged

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

如果您的List和ComboBox没有共享相同的数据结构,则可以执行以下操作:

您的列表数据结构:

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

您的ComoboBox数据结构:

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

比在列表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