简体   繁体   中英

“Binding” wpf Combox selectedValue to an integer?

I just started a new wpf project in hopes that I could learn a new technique as opposed to using winForms all the time.

I seem to be having way too much difficulty binding the selected value of a comboBox to an integer variable in my "MainWindow" class.

I have been looking at a host of "simple" examples from sites like codeproject, but they all seem way too complicated to just return the selected value of a comboBox. I am used to setting the "SelectedValueChanged" property and just setting a variable, which takes just a few clicks, like so:

public int value;

public void comboBox_SelectedValueChanged()
{
    value = comboBox.SelectedValue();
}

Is there a similarly sweet, simple, and short way to properly "bind" the selected comboBox item to an integer?

I am trying to understand how to use INotifyPropertyChanged but I keep getting errors when I try to use it. This is what I have so far, but to be honest, I'm not sure where I am going with it:

    // Combo Box Value
public class ComboValue
{
    #region Members
    int valueToReturn;
    #endregion 

    # region Properties
    public int numWeeks
    {
        get { return valueToReturn; }
    }
    #endregion
}

// Veiw Model Class
public class ComboValueViewModel:INotifyPropertyChanged
{
    #region Construction

    public ComboValueViewModel()
    {

    }

    #endregion
}

and I've never used "#region" before, I have no clue what that is.

Could someone fill me in if I'm headed down the right path here?

You don't mention how much you know of MVVM but here goes. Your view will have an associated ViewModel class. In here you'll expose a property containing the items to bind to the combobox, eg:

public List<ComboValue> ComboItems { get; set; }

If you populate this property in the VM's constructor, then a List<> is probably sufficient; however you'll often see an ObservableCollection<> used for this kind of thing - this comes into its own if you need to add or remove items within your VM code - your view will react to such changes and update the list accordingly. This won't happen with a List<>.

As for INotifyPropertyChanged, I haven't implemented this pattern in the above code snippet. Again, it's not strictly necessary if you populate the collection in the VM constructor and won't be re-assigning that property again . However it's good practice to use the INPC pattern on your VM properties. Without it, if you were to reassign that property elsewhere in your code, eg:-

ComboItems = aNewListOfItems;

then the view wouldn't be made aware of the property change, and the ComboBox wouldn't update. If you need this to happen then implement the INPC pattern on the property, eg:-

public List<ComboValue> ComboItems   // or ObservableCollection<ComboValue>
{
    get 
    { 
        return _comboItems;
    }
    set
    {
        if (_comboItems != value)
        {
            _comboItems = value;
            OnPropertyChanged("ComboItems");
        }
    }
}

As you are working with a ComboBox, your VM should also expose a property that you bind to the control's SelectedItem property. This property should implement INPC, eg:-

public ComboValue SelectedItem
{
    get 
    { 
        return _selectedItem;
    }
    set
    {
        if (_selectedItem != value)
        {
            _selectedItem = value;
            OnPropertyChanged("SelectedItem");
        }
    }
}

As you select items in the combo, the VM's SelectedItem property will change to reflect the current selection.

Finally, your XAML should end up looking something like this:-

<ComboBox ItemsSource="{Binding ComboItems}" SelectedItem="{Binding SelectedItem}" />

Hope this gives you a little "primer" into WPF binding! (Code snippets taken from memory so may not be 100% correct!).

Edit Your ComboValue class exposes a numWeeks property. As it stands, you'll probably find that your ComboBox displays a list of ComboValue type names. To get the number to appear, the easiest thing is just to override .ToString() in your class and return the value of numWeeks . For more advanced formatting of items in controls such as this, you'll typically specify an ItemTemplate (again, plenty of examples can be found via Google!).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM