My problem is that I'm trying to bind between a ComboBox
property type and a string
viewModel, but I can't do it
<ComboBox HorizontalAlignment="Stretch"
SelectedItem="{Binding Path=OResultado, Mode=TwoWay}">
<ComboBoxItem Content="Anomalia"/>
<ComboBoxItem Content="Retiro"/>
<ComboBoxItem Content="Retiro con Anomalia"/>
<ComboBoxItem Content="Conforme"/>
</ComboBox>
I tried with SelectedItem
and SelectedValue
, SelectedIndex
but it returns only the index of the ComboBoxItem
selected one. I tried to do a Converter
but I couldn't get it to return the expected result.
What I want to do is that when the user selects an item from ComboBox
the item's value Content
is assigned to the type property string OResultado
of my viewModel.
In this case there are several ways to do it, but the easiest way is to populate the ComboBox dynamically instead of with static values.
This is achieved by creating the list of options in the
ViewModel
and doingBinding
with theItemsSource
delComboBox
.view model
you can get BindableBase here: Apps, Binding, INotifyPropertyChanged and BindableBase | XAML | C# , I have set the value of
OResultado= "Conforme"
.Additionally, I have performed the trick of lazy initialization with the call to the properties to be able to create the instance of
viewmodel
from thexaml
XAML
already having the viewModel then you add it in your
xaml
asDataContext
and then do binding of the
ItemsSource
conListaOpciones
that is the property exposed in the ViewModel, at once then you assignSelectedItem
to do binding withOResultado
inTwoWay Mode
Remember to add the reference to the viewmodel's namespace (line 5 ).
This is all!
And as I always tell you, you have to learn Binding, I recommend the
XAML video course especially chapters 5,7,8