Mike Diaz
Mike Diaz

Reputation: 2065

set SelectedIndex of Combobox in WPF to 0

I am binding a Collection at run time to a Combobox and I would like to set the Index after to 0. I could not find a straight answer to what I want.

_stationNames = new ObservableCollection<string>(_floorUnits.Unit.Select(f => f.Name));
_stationNames.Insert(0, "All");
stationsComboBox.ItemsSource = _stationNames;
stationsComboBox.SelectedIndex = 0;//Doesn;t work

Xaml

<ComboBox x:Name="stationsComboBox" Grid.Row="1" Grid.Column="1" Text="{Binding Name}"
                  SelectionChanged="StationComboBoxSelectionChanged" VerticalAlignment="Center" Margin="3"
                   SelectedIndex="0"/>

Upvotes: 0

Views: 3603

Answers (1)

Steve Py
Steve Py

Reputation: 34783

It sounds like you're trying to use it like you would with WinForms. WPF is a slightly different beast and a lot more powerful regarding bindings.

I recommend reading a bit on MVVM to get the most benefit from WPF. By binding the XAML to a view model class (rather than trying to wire things up in Code-behind) you will find you can accomplish what you want with a lot more flexibility without oodles of code.

For instance: Given the following VM:

public class MyViewModel: INotifyPropertyChanged
{

    public ObservableCollection<string> StationNames
    {
        get;
        private set;
    }

    public Something()
    {
        StationNames = new ObservableCollection<string>( new [] {_floorUnits.Unit.Select(f=>f.Name)});
        StationNames.Insert(0, "All");
    }

    private string _selectedStationName = null;
    public string SelectedStationName
    {
        get
        {
            return _selectedStationName;
        }
        set
        {
            _selectedStationName = value;
            FirePropertyChanged("SelectedStationName");
        }
    }

    private void FirePropertyChanged(string propertyName)
    {
        if ( PropertyChanged != null )
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

You can set your view's (XAML form) DataContext to an instance of the ViewModel and update your combo box definition to:

<ComboBox x:Name="stationsComboBox" Grid.Row="1" Grid.Column="1"
                  ItemsSource="{Binding Path=StationNames}" SelectedItem={Binding Path=SelectedStationName} VerticalAlignment="Center" Margin="3"
                   SelectedIndex="0"/>

From here whenever the combo box selection changes, the VM's SelectedStationName updates to reflect the current selection, and from anywhere in the VM code, setting the VM's SelectedStationName will update the combo's selection. (I.e. implementing a Reset button, etc.)

Normally though, with something like what you've suggested, I would be looking at binding directly to the Units collection. (or VM's derived from units if they themselves can be viewed/edited.) In any case it should give you a bit of a starting point to start researching into WPF bindings.

Upvotes: 1

Related Questions