Calling a method from a combo box selection?

DoJa

Active member
Joined
Mar 23, 2014
Messages
33
Programming Experience
1-3
I have a project with a ComboBox which lists each day of the week. The user should be able to select a day of the week from the combobox drop down to make 'stuff happen'.

In the code behind I have a method which accepts a string overload for the day of week

eg.
C#:
update("Monday");

I know how do do this using events .. "the winforms way", but I would like to do it properly , "the wpf way" using databinding.

My question then (I think!), is how can I create a data binding which will call a method in the model after making a change in the view?

Is it even possible to do this directly or does it have to be approached differently?
 
That turned out to be a little easier than expected. Couldn't find anything particularly useful from extensive Google searching but a bit of experimentation yielded the desired result.

Was able to get the desired result in 3 steps.

1. Put method call beneath OnpropertyChanged for SelectedValue

2. Set SelectedValuePath="Content" to get the actual value rather than the full system.windows.etc path

3. Set Bindingmode to two way.

The only issue I have now is that the combobox defaults to a blank/empty/non selected state. Does anyone know how I can add some default 'Please Select' text or specify an index to start at upon loading?
 
Back
Top Bottom