2

I have a combobox inside a list where the items of the combo box are dynamic. They will be determined by the help of the selected item.

For example: If selected item is Item1 then combobox should contain Item1, Item2, Item3 but if selected item is Item2 then combo box should contain Item2, Item3, Item4

How to achieve the same by using binding..

Right now i am setting up two properties in my collection called SelectedValue and ListValues and binding them with my combobox but it only select first item of the list and leaving the rest one as it is.

Also, what is the order of execution of data binding as i want first Itemsource to call then selectedvalue should be set so that items will be selected.

Really appreciate for any help.

Here is my effort which works out but i am not sure if it's correct or not.

C#

public string SelectedValue
{
    get
    {
        PropertyChanged.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs("Values"));
        return _value;
    }
    set
    {
        if (value != null) //It will be null when binding of values happens
        { 
            _value = value;
            PropertyChanged.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs("Values"));
            PropertyChanged.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs("SelectedValue"));
        }
    }
}

public IList<SomeType> Values
{
    get
    {
        string status =_status;
        return SomeFunctionToReturnValues(status);
    }
}

XAML

<ComboBox SelectedValue="{Binding SelectedValue}" SelectedValuePath="Id" DisplayMemberPath="Text" Width="120" ItemsSource="{Binding Values,Mode=OneWay}"></ComboBox>

Please comment and Let me know if anyone can provide me better suggestion here.

Yael
  • 1,566
  • 3
  • 18
  • 25
Deep Panda
  • 88
  • 9
  • Bind the `ObservableCollection` and `Item` selected of VM to the `ItemsSource` and `SelectedItem` of `Combobox` in view respectively. Change the collection based on the changes in `SelectedItem` – Gopichandar Apr 06 '16 at 07:53
  • The order is itemssource first then selecteditem, you cant have selected item if there is no itemssource. – adminSoftDK Apr 06 '16 at 08:09
  • @Gopichandar The issue is that it's the order of the ItemSource and SelectedItem that will determine if it works or not. So, If ItemSource calls first them SelectedItem is old and lost and if ItemSource is second then SelectedItem will not work! – Deep Panda Apr 07 '16 at 06:19
  • Did you resolved the issue? – Noam M Jul 20 '17 at 05:37

1 Answers1

1

You shold use ItemsSource property

<ComboBox SelectedItem="{Binding MyNum}" ItemsSource="{Binding Numbers}" Width="100" Height="30"/>

That bound to:

// Fills up combo box
public IEnumerable<int> Numbers
{
    get
    {
        IEnumerable<int> temp = MyCollection.ToList(); 
        return temp.SubArray(MyNum,MyCollection.count);
    }
}

private int _myNum
public int MyNum
{
    get
    {
        return  _myNum;
    }
    set
    {
        _myNum = value;
        OnPropertyChanged("MyNum");
    }
}

When the SubArray is (Credit)

public static T[] SubArray<T>(this T[] data, int index, int length)
{
    T[] result = new T[length];
    Array.Copy(data, index, result, 0, length);
    return result;
}
Community
  • 1
  • 1
Noam M
  • 3,156
  • 5
  • 26
  • 41
  • The order here will be something which needs to be prioritized. This is kind of tricky as the original item is not getting selected as binding with next collection lost the selected item and thus drop down is deselected. – Deep Panda Apr 07 '16 at 06:22