在 WPF ComboBox 中设置所选项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33579804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set selected item in WPF ComboBox
提问by John
(I've read similar posts but they all had a twist to them that made the solution different)
(我读过类似的帖子,但它们都有不同之处,使解决方案有所不同)
I'm porting a WinForms app that used this:
我正在移植一个使用它的 WinForms 应用程序:
myComboBox.SetSelected(myComboBox.FindString("Some Text"), true);
to select an item programmatically. When porting over to WPF, I tried this but it has no effect (the item does not get selected):
以编程方式选择一个项目。移植到 WPF 时,我尝试了此操作,但没有效果(未选择该项目):
myComboBox.SelectedItem = myComboBox.FindName("Some Text");
What is the correct way to select an existing item in a ComboBox, in WPF?
在 WPF 的 ComboBox 中选择现有项目的正确方法是什么?
回答by AnjumSKhan
You have to use SelectedValue. In WPF ComboBox, there are multiple ways to achieve the same thing. So, one syntax to select an item programmatically won't work. There are various ways of adding items to ComboBox.
您必须使用 SelectedValue。在 WPF ComboBox 中,有多种方法可以实现相同的目的。因此,以编程方式选择项目的一种语法将不起作用。有多种方法可以将项目添加到 ComboBox。
- You can set ItemsSource both declaratively or in code.
- You can add ComboBoxItems etc. See Items property in property window to see various item-types available.
- 您可以以声明方式或在代码中设置 ItemsSource。
- 您可以添加 ComboBoxItems 等。请参阅属性窗口中的 Items 属性以查看各种可用的项目类型。
If you are using ItemsSource with string values, then you need syntax like : cmb1.SelectedValue = "Name1"
如果您使用带有字符串值的 ItemsSource,那么您需要如下语法: cmb1.SelectedValue = "Name1"
If you are directly adding items like <ComboBox ...> <ComboBoxItem Content="Name1"/> </ComboBox/>, then you need
如果您直接添加诸如 之类的项目<ComboBox ...> <ComboBoxItem Content="Name1"/> </ComboBox/>,那么您需要
foreach (ComboBoxItem item in cmb2.Items)
if (item.Content.ToString() == "Name1")
{
cmb2.SelectedValue = item;
break;
}
I have posted a full working sample demonstrating how to select an item programmatically in various scenarios. Sample code (can be used as is) :
我发布了一个完整的工作示例,演示如何在各种场景中以编程方式选择项目。示例代码(可以按原样使用):
Pay attention to last one, where you have to use SelectedValuePath.
注意最后一个,你必须使用 SelectedValuePath。
Window1.xaml
窗口1.xaml
<Window x:Class="WpfApplicationBlend.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="411" Width="749">
<Grid>
<Grid Margin="30,27,491,276">
<ComboBox x:Name="cmb1" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}">
<ComboBox.ItemsSource>
<CompositeCollection>
<sys:String>Name1</sys:String>
<sys:String>Name2</sys:String>
<sys:String>Name3</sys:String>
<sys:String>Name4</sys:String>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
<TextBox x:Name="tbInput1" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/>
</Grid>
<Grid Margin="405,27,111,276">
<ComboBox x:Name="cmb2" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}">
<ComboBoxItem Content="Name1"/>
<ComboBoxItem Content="Name2"/>
<ComboBoxItem Content="Name3"/>
</ComboBox>
<TextBox x:Name="tbInput2" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button2_Click"/>
</Grid>
<Grid Margin="30,207,491,96">
<ComboBox x:Name="cmb3" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}">
<ComboBox.ItemsSource>
<CompositeCollection>
<sys:String>Name1</sys:String>
<sys:Boolean>True</sys:Boolean>
<sys:Int32>123</sys:Int32>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
<TextBox x:Name="tbInput3" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button3_Click"/>
</Grid>
<Grid Margin="405,207,116,96">
<ComboBox x:Name="cmb4" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" SelectedValuePath="Name" DisplayMemberPath="Name">
</ComboBox>
<TextBox x:Name="tbInput4" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button4_Click"/>
</Grid>
</Grid>
</Window>
Window1.xaml.cs
Window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections;
namespace WpfApplicationBlend
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<Employee> employees = new List<Employee>()
{
new Employee(){Name="Name1", Age=100},
new Employee(){Name="Name2", Age=101},
};
cmb4.ItemsSource = employees;
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
cmb1.SelectedValue = tbInput1.Text;
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
foreach (ComboBoxItem item in cmb2.Items)
if (item.Content.ToString() == tbInput2.Text)
{
cmb2.SelectedValue = item;
break;
}
}
private void Button3_Click(object sender, RoutedEventArgs e)
{
foreach (object item in cmb3.Items)
if (item.ToString() == tbInput3.Text)
{
cmb3.SelectedValue = item;
break;
}
}
private void Button4_Click(object sender, RoutedEventArgs e)
{
cmb4.SelectedValue = tbInput4.Text;
}
}
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
}
}
回答by Adolf
comboboxName.SelectedIndex = yourIndex;
e.g.
例如
combobox1.SelectedIndex = 2;

