Make It Perfect
Make It Perfect

Reputation: 995

Wpf's ComboBox not firing SelectedIndex

I have a scenario where combobox can have same string values. for exa combo box can have following values in drop down: "Test", "Test1", "Test1", "Test1", "Test2",

On the basis of selected index I am filling another combobox. My Xaml Looks like:

<Grid >
    <Grid.RowDefinitions>
        <RowDefinition Height="40"></RowDefinition>
    </Grid.RowDefinitions>
    <ComboBox ItemsSource="{Binding Path=ComboList, Mode=OneWay}"
              SelectedIndex="{Binding Path=ComboIndex, Mode=TwoWay}"/ >
</Grid>

ViewModel looks like:

class TestViewModel : INotifyPropertyChanged
{
    private IList<string> _comboList = new List<string>
                                      {
                                          "Test",
                                          "Test1",
                                          "Test1",
                                          "Test1",
                                          "Test2",
                                      };       

    public IList<string> ComboList
    {
        get { return _comboList; }
    }


    private int _comboIndex;

    public int ComboIndex
    {
        get { return _comboIndex; }
        set
        {
            if (value == _comboIndex)
            {
                return;
            }

            _comboIndex = value;
            OnPropertyChanged("ComboIndex");
        }
    }

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

    public event PropertyChangedEventHandler PropertyChanged;
}

Problem I am facing is that SelectedIndex does not get fired incase I am suffling between same string value (like changing value from "Test1" ,present at index 1, to "Test1", present at index 2.

Upvotes: 0

Views: 929

Answers (2)

blindmeis
blindmeis

Reputation: 22445

when i need such relations i create the relations in my viewmodel and simply bind to this collection

 public class MyItem
 {
    public string Name {get; set;}//your Test, Test1, Test1 ...
    public List<string> Childs {get; set;} // the childs depending on the the Name
 }

in your viewmodel you can now create your List of MyItem and fill it like you need.

 public List<MyItem> MyItemList {get;set;}

in xaml you can now simply create your related comboboxes.

 <ComboBox ItemsSource="{Binding Path=MyItemList}"
          SelectedItem="{Binding Path=ComboIndex, Mode=TwoWay}"/ >

 <ComboBox ItemsSource="{Binding Path=ComboIndex.Childs}"
          SelectedItem="{Binding Path=MySelectedPropForChild, Mode=TwoWay}"/ >

so you dont have to care of any index cause you already build your relations.

Upvotes: 1

Phil
Phil

Reputation: 43021

Instead of binding to a List<string>, encapsulate the strings, e.g.

public class Item
{
    public Item(string v){ Value = v; }
    public string Value{get; private set;}
}

and bind to a List<Item>.

Then amend your Xaml to specify the DisplayMemberPath

<ComboBox ItemsSource="{Binding Path=ComboList, Mode=OneWay}"  
          DisplayMemberPath="Value"
          SelectedIndex="{Binding Path=ComboIndex, Mode=TwoWay}"/ >  

That worked for me.

Upvotes: 0

Related Questions