簡體   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