Ivan
Ivan

Reputation: 1171

How to select combobox item automatically and programatically?

I have an array of combo boxes, and once each combo box is populated with items, I want the first item to be selected automatically. SO I do this:

all_transition_boxes[slide_item].SelectedItem = all_transition_boxes[slide_item].Items[0];

but then later I can not change the index anymore if I want to select some other item. It seems that the index is permanently set to zero. I tried to use SelectedItem instead of SelectedIndex but it doesn't work at all. I would appreciate any help.

  //populate each combobox with corresponding elements
                for (int i = 0; i < slide_transitions.Count; i++)
                {
                    all_transition_boxes[slide_item].Items.Add("Transition " + (i + 1));


                }


all_transition_boxes[slide_item].SelectedItem = all_transition_boxes[slide_item].Items[0];

Upvotes: 0

Views: 981

Answers (2)

XAMeLi
XAMeLi

Reputation: 6289

On your ComboBox (in XAML) set:

SelectedIndex = "0"

You can set it as a setter in a style that is applied to all instances of ComboBox in your array.

Upvotes: 0

Anon Dev
Anon Dev

Reputation: 1411

I have created a sample code to replicate your issue, please check it.

A form with a combobox and two buttons:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Activated="Window_Activated">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="329*"/>
            <ColumnDefinition Width="34*"/>
            <ColumnDefinition Width="154*"/>
        </Grid.ColumnDefinitions>
        <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="146,78,0,0" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="101,185,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
        <Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="266,185,0,0" VerticalAlignment="Top" Width="75" Grid.ColumnSpan="2" Click="button1_Click"/>
    </Grid>
</Window>

And the form´s code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            var items = new List<string>();

            for (var i = 0; i < 10; i++)
            {
                items.Add("Item" + i);
            }

            comboBox.ItemsSource = items;
            comboBox.SelectedItem = "Item0";
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            comboBox.SelectedItem = "Item5";
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            comboBox.SelectedItem = "Item9";
        }
    }
}

Upvotes: 1

Related Questions