I have an application that uses WPF using MVVM with a ComboBox. When a certain value is selected, a textbox must be disabled, the XAMl has it like this: this is the textbox that is required to be enabled
<TextBox Name="TCRM" Grid.Row="11" Grid.Column="1"
Width="100" Height="18"
HorizontalAlignment="Left"
VerticalAlignment="Center"
helpers:TextFilterService.TextBoxFilter="Money"
FlowDirection="RightToLeft" MaxLength="4"
Margin="10,2,0,1" IsEnabled="{Binding ElementName=cbAgregarManual, Path=IsChecked}"
Text="{Binding Path=Concepto.TCRM, ValidatesOnNotifyDataErrors=True, ValidatesOnExceptions=True, Mode=TwoWay}"/>
The combobox that determines whether to enable or disable:
<ComboBox Name="ComboTipoPpa" Grid.Row="10" Grid.Column="1"
Height="26" Width="177"
HorizontalAlignment="Left"
Margin="10,1,0,1"
IsEnabled="{Binding ElementName=cbAgregarManual, Path=IsChecked}"
ItemsSource="{Binding Path=TipoPpaTos, ValidatesOnNotifyDataErrors=False}"
SelectedItem="{Binding Path=SelectedTipoPpaTO, Mode=TwoWay, NotifyOnValidationError=True}" DisplayMemberPath="Nombre"
SelectedValuePath="ValoresPPA"/>
if you select STR or TR (Representative Rate) the control enable must be triggered. THIS IS THE PART I can't find to perform, in MVVM.
Something like if TR is selected in the combobox, the TextBox Name="TCRM" is enabled, otherwise it is not enabled
As Leodev says in the comments, there are several ways to fix this problem, one of the easiest is to use a boolean.
To explain it to you, I will use an example.
Assuming we have the following ViewModel
Then in xaml
In this way, when changing the option in the ComboBox, the Setter will be executed
NumeroSeleccionado
, therefore, the methodOnPropertyChanged
, which will update our view, will be executed.When executed, the property will be recalculated
TextBoxActivo
, which is bound toIsEnabled
the TextBox.Therefore, if 2 or 4 is selected in the Combo, the TextBox will be active, otherwise, it will not be.
Let's see... I did this to the eye... According to my comment, what I would do is something like this:
First: Create a
Converter
that receives theSelectedItem
(the entire object)Then you add
namespace
your converters to the window and create an instance of the newly made converter:Then the
TextBox
one you want to disable you control the "IsEnabled
" with the SelectedItem of the ViewModel directly, which I think is SelectedTipoPpaTO (or with the SelectedItem of the ComboBox... but if you have the object at hand... better use the object) and send that data to the converter that will return a true or a false depending on the criteria you want to define. I did something like this for the example:And that's it.. Maybe I misunderstood? Well... I'll leave you the example, tell me if it works for you or I'll modify it.
All the best