以编程方式在WPF(3.5sp1)中设置ComboBox SelectedItem

Vic*_*or 11 wpf combobox selecteditem

在安装了Net Framework 3.5 sp1的wpf应用程序中设置SelectedItem programmaticaly时,我感到很困惑.我仔细阅读了大约一百个帖子\主题但仍然困惑((我的xaml:

 <ComboBox name="cbTheme">
    <ComboBoxItem>Sunrise theme</ComboBoxItem>
    <ComboBoxItem>Sunset theme</ComboBoxItem> 
 </ComboBox>
Run Code Online (Sandbox Code Playgroud)

如果我在其中一个项目中添加IsSelected ="True"属性 - 它不会设置此项目.为什么?而我在代码中尝试不同,仍然无法设置所选项目:

cbTheme.SelectedItem=cbTheme.Items.GetItemAt(1); //dosn't work
cbTheme.Text = "Sunrise theme"; //dosn't work
cbTheme.Text = cbTheme.Items.GetItemAt(1).ToString();//dosn't work
cbTheme.SelectedValue = ...//dosn't work
cbTheme.SelectedValuePath = .. //dosn't work
//and even this dosn't work:
ComboBoxItem selcbi = (ComboBoxItem)cbTheme.Items.GetItemAt(1);//or selcbi = new ComboBoxItem
cbTheme.SelectedItem = selcbi;
Run Code Online (Sandbox Code Playgroud)

SelectedItem不是readonly属性,为什么它不工作?我认为这应该是微软的问题,而不是我的问题.或者我错过了什么?我尝试使用ListBox,并且所有工作都可以正常使用相同的代码,我可以设置选择,获取选择等等......那么我可以用ComboBox做什么?也许一些技巧???

小智 9

要选择和中的任何项目ComboBox并将其设置为选中的默认项目,请使用以下行:

combobox.SelectedIndex = 0; //index should be the index of item which you want to be selected
Run Code Online (Sandbox Code Playgroud)


小智 7

如果我以编程方式添加组合框和项目,这对我有用:

ComboBox newCombo = new ComboBox();

ComboBoxItem newitem = new ComboBoxItem();
newitem.Content = "test 1";
newCombo.Items.Add(newitem);
newitem = new ComboBoxItem();
newitem.Content = "test 2";
newCombo.Items.Add(newitem);
newitem = new ComboBoxItem();
newitem.Content = "test 3";
newCombo.Items.Add(newitem);

newCombo.SelectedItem =  ((ComboBoxItem)newCombo.Items[1]);
newCombo.Text = ((ComboBoxItem)newCombo.Items[1]).Content.ToString();

newStack.Children.Add(newCombo);
Run Code Online (Sandbox Code Playgroud)

如果以ItemSource编程方式设置属性,然后将文本设置为选定值,它也可以工作.


iha*_*ash 5

在viewmodel中为主题列表创建一个公共属性,为所选项创建一个公共属性:

    private IEnumerable<string> _themeList;
    public IEnumerable<string> ThemeList
    {
        get { return _themeList; }
        set
        {
            _themeList = value;
            PropertyChangedEvent.Notify(this, "ThemeList");
        }
    }
    private string _selectedItem;
    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            PropertyChangedEvent.Notify(this,"SelectedItem");
        }            
    }
Run Code Online (Sandbox Code Playgroud)

将你的组合框在xaml中绑定到如下属性:

    <ComboBox 
        Name="cbTheme" 
        ItemsSource="{Binding ThemeList}"      
        SelectedItem="{Binding SelectedItem}">
    </ComboBox>
Run Code Online (Sandbox Code Playgroud)

现在,您只需将项添加到ThemeList以填充组合框.要选择列表中的项目,请将selected属性设置为要选择的项目的文本,如下所示:

    var tmpList = new List<string>();
    tmpList.Add("Sunrise theme");
    tmpList.Add("Sunset theme");

    _viewModel.ThemeList = tmpList;
    _viewModel.SelectedItem = "Sunset theme";
Run Code Online (Sandbox Code Playgroud)

或者尝试将所选项目设置为您想要在自己的代码中选择的项目的字符串值,如果您想使用您当前拥有的代码 - 不确定它是否可行,但您可以尝试.

  • 这应该是公认的答案,它是唯一可以测试的单元 (2认同)

JTe*_*Tew 0

ComboBox 是否绑定数据?

如果是这样,您可能最好通过绑定而不是代码来完成......

看到这个问题... WPF ListView 以编程方式选择项目

也许创建一个新的 SelectableObject {Text = "Abc Theme", IsCurrentlySelected = True} 将 SelectableObject 的集合绑定到 ComboBox。

本质上是在模型中设置 IsCurrentlySelected 属性并从模型更新 UI。