C# 如何将ListView控件绑定到ComboBox ItemSource

C# 如何将ListView控件绑定到ComboBox ItemSource,c#,uwp,uwp-xaml,C#,Uwp,Uwp Xaml,我需要将两个ListView控件添加到ComboBox项中,但它显示的是ListView项,而不是ListView名称 listView = new ListView(); ObservableCollection<String> list = new ObservableCollection<string>(); list.Add("1"); list.Add("2"); listView.ItemsSource = list; listView2 = n

我需要将两个
ListView
控件添加到
ComboBox
项中,但它显示的是ListView项,而不是ListView名称

listView = new ListView();
ObservableCollection<String> list = new ObservableCollection<string>();
list.Add("1");
list.Add("2");      
listView.ItemsSource = list;

listView2 = new ListView();
ObservableCollection<String> list12 = new ObservableCollection<string>();
list12.Add("11");
list12.Add("12");
listView2.ItemsSource = list12;

combobox.Items.Add(listView);
combobox.Items.Add(listView12);

private void combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   var combobox = sender as ComboBox;
   if(combobox.SelectedIndex == 0)
      combobox.ItemsSource = listView;
   else
      combobox.ItemsSource = listView12;
}
listView=newlistview();
ObservableCollection列表=新的ObservableCollection();
列表。添加(“1”);
列表。添加(“2”);
listView.ItemsSource=列表;
listView2=新建ListView();
ObservableCollection list12=新的ObservableCollection();
清单12.添加(“11”);
清单12.添加(“12”);
listView2.ItemsSource=list12;
combobox.Items.Add(listView);
combobox.Items.Add(listView12);
private void组合框\u SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
var combobox=发送方作为组合框;
如果(combobox.SelectedIndex==0)
combobox.ItemsSource=listView;
其他的
combobox.ItemsSource=listView12;
}
这是我的Xaml代码

<ComboBox Grid.Row="4" x:Name="combobox" SelectionChanged="combobox_SelectionChanged" />

您的错误出现在以下几行:

if(combobox.SelectedIndex == 0)
  combobox.ItemsSource = listView;
else
  combobox.ItemsSource = listView12;
combobox.Items.Add(listView);
combobox.Items.Add(listView12);
您正在将列表内容设置为组合框的
ItemsSource
。这意味着组合框中的选项将是
列表视图
列表视图12
中的项目

您不应该有任何事情要做,因为您已经在启动期间使用以下行填充了combobox:

if(combobox.SelectedIndex == 0)
  combobox.ItemsSource = listView;
else
  combobox.ItemsSource = listView12;
combobox.Items.Add(listView);
combobox.Items.Add(listView12);
当组合框选择更改时,您可能只想检索所选项目并用它更新应用程序的其他部分。

listView=new listView();
listView = new ListView();
ObservableCollection<String> list = new ObservableCollection<string>();
list.Add("1");
list.Add("2");      
listView.ItemsSource = list;

listView2 = new ListView();
ObservableCollection<String> list12 = new ObservableCollection<string>();
list12.Add("11");
list12.Add("12");
listView2.ItemsSource = list12;

Dictionary<string,ObservableCollection<String>> dictionary = new Dictionary<string,ObservableCollection<String>();
dictionary.Add(nameof(list), list);
dictionary.Add(nameof(list12),list12);

combobox.ItemsSource = dictionary;
//And make sure you add DisplayMemberPath="Key" in the combobox.

private void combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   var combobox = sender as ComboBox;
   if(combobox.SelectedIndex == 0)
      combobox.ItemsSource = dictionary[nameof(list)];
   else
      combobox.ItemsSource = dictionary[nameof(list12)];

   //Clear the DisplayMemberPath since you're binding the values now.
   combobox.DisplayMemberPath  = null;
}
ObservableCollection列表=新的ObservableCollection(); 列表。添加(“1”); 列表。添加(“2”); listView.ItemsSource=列表; listView2=新建ListView(); ObservableCollection list12=新的ObservableCollection(); 清单12.添加(“11”); 清单12.添加(“12”); listView2.ItemsSource=list12;
Dictionary Dictionary=新建字典您是否尝试过创建一个可以将密钥绑定到ComboBox的字典?我尝试过了,它不起作用。。我需要确切的答案