Search code examples
c#wpfxamlmvvmcombobox

Changing selected index of combo box with databinding in WPF C#


I am trying to make the conversion from Winforms to WPF in c#. I am struggling a bit with WVVM and data binding. I admit I don't have a full grasp on the way everything works together but I think i just need more practice.

I used this article on codeproject to help me understand how to convert my local address book application from WinForms to WPF.

While I can get data binding to work fine with int and string I am struggling with a ComboBox. The data is stored in an xml file and stored as strings and int's in the contacts class. The ComboBox should auto update when a location is selected from a list box. I have this all working for properties that allow user custom information. This particular setting needs to be a drop down for pre-set values.

How do I convert the string and pass the SelectedIndex[] property to the comboBox through data binding?

I omitted some of the extra working code used in string population for text boxes to clean it up some.

XAML

<Window
    <Window.DataContext>
        <viewmodels:mainviewModel x:Name="viewModel1"/>
    </Window.DataContext>
    <Grid Background="#FF8B8686">
        <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="174" Margin="44,77,0,0" VerticalAlignment="Top" Width="164" ItemsSource="{Binding Contacts}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedContact}" SelectionChanged="listBox_SelectionChanged" />
        <TextBox x:Name="textBoxName" HorizontalAlignment="Left" Height="23" Margin="244,77,0,0" TextWrapping="Wrap" Text="{Binding SelectedContact.Name}" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="textBoxAddress" HorizontalAlignment="Left" Height="23" Margin="244,120,0,0" TextWrapping="Wrap" Text="{Binding SelectedContact.Address}" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="textBoxBandwidth" HorizontalAlignment="Left" Height="23" Margin="244,198,0,0" TextWrapping="Wrap" Text="{Binding SelectedContact.Bandwidth}" VerticalAlignment="Top" Width="120" DataContext="{Binding Mode=OneWay}"/>
        <Button x:Name="button1" Content="Write Data" HorizontalAlignment="Left" Margin="194,271,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
        <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="267,229,0,0" VerticalAlignment="Top" Width="120" SelectedItem="{Binding SelectedContact.Combo}">
            <ComboBoxItem Content="1" />
            <ComboBoxItem Content="2" />
            <ComboBoxItem Content="3" />
            <ComboBoxItem Content="4" />
            </ComboBox>
    </Grid>
</Window>

Contact.cs

public class Contact : ViewModelEntity
{
    public Contact()
    {
    }

    protected int combo;
    public int Combo
    {
        get { return combo; }
        set
        {
            if (combo != value)
            {
                switch (value)
                {
                    case 1:
                        combo = ComboBox.SelectedIndex[0];
                        break;
                    case 2:
                        combo = ComboBox.SelectedIndex[1];
                        break;
                    case 3:
                        combo = ComboBox.SelectedIndex[2];
                        break;
                    case 4:
                        combo = ComboBox.SelectedIndex[3];
                        break;
                    default:
                        combo = ComboBox.SelectedIndex[0];
                        break;
                }
                // combo = value;
                NotifyPropertyChanged("Combo");
            }
        }
    }

mainviewModel.cs

using System.Collections.ObjectModel;

namespace WpfApplication1.viewmodels
{
    public class mainviewModel:ViewModelEntity
    {

       public mainviewModel()
        { }
        protected Contact selectedContact = null;

        protected ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();

        public ObservableCollection<Contact> Contacts
        {
            get { return contacts; }
            set { contacts = value; }
        }


        public Contact SelectedContact
        {
            get { return selectedContact; }
            set {
                if (selectedContact != value)
                {
                    selectedContact = value;
                    NotifyPropertyChanged("SelectedContact");
                }


            }
        }
    }
}

Solution

  • Why don't you directly bind it to the SelectedIndex directly?

        <ComboBox x:Name="comboBox"
                  Width="120"
                  Margin="267,229,0,0"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  SelectedIndex="{Binding SelectedContact.Combo}">
            <ComboBoxItem Content="1" />
            <ComboBoxItem Content="2" />
            <ComboBoxItem Content="3" />
            <ComboBoxItem Content="4" />
        </ComboBox>
    

    In code behind :

    protected int combo;
    public int Combo
    {
        get { return combo; }
        set
        {
            combo = value;
            NotifyPropertyChanged("Combo");        
        }
    }