在WPF ComboBox中设置选定项目

4
我已经阅读了类似的帖子,但它们都有一个扭曲的地方,使解决方案不同。
我正在移植一个使用以下代码的WinForms应用程序:
myComboBox.SetSelected(myComboBox.FindString("Some Text"), true);

以编程方式选择一个项目。当迁移到 WPF 时,我尝试了这个方法,但它没有效果(项目没有被选中)。
myComboBox.SelectedItem = myComboBox.FindName("Some Text");

在WPF中,选择ComboBox中现有项目的正确方法是什么?

你的 ItemSource 是什么? - Sajeetharan
1
答案取决于你的代码,但你没有提供。即使你展示的Winforms示例也不是一个很好的方法。在WPF中,你应该有一个绑定到ComboBox.ItemsSource的集合,并且你可以将SelectedItem设置为该集合元素的引用。如何查找它将取决于代码的具体实现。 - Peter Duniho
2个回答

4
您需要使用SelectedValue。在WPF ComboBox中,有多种实现相同功能的方法。因此,一种以编程方式选择项目的语法不起作用。有各种添加项目到ComboBox的方法。
1.您可以在声明式或代码方式下设置ItemsSource。 2.您可以添加ComboBoxItems等。请查看属性窗口中的Items属性以查看可用的各种项类型。
如果您正在使用带有字符串值的ItemsSource,则需要类似如下的语法:cmb1.SelectedValue = "Name1" 如果您直接添加项,例如<ComboBox ...> <ComboBoxItem Content="Name1"/> </ComboBox/>,则需要...
foreach (ComboBoxItem item in cmb2.Items)
      if (item.Content.ToString() == "Name1")
      {
         cmb2.SelectedValue = item;
         break;
      }

我已经发布了一个完整的工作示例,展示了如何在各种场景下以编程方式选择项目。示例代码(可以直接使用):

请注意最后一个示例,其中您需要使用SelectedValuePath。

Window1.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

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

0
comboboxName.SelectedIndex = yourIndex; 

例如

combobox1.SelectedIndex = 2;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接