ComboBox ignoring IsEnable binding changes
See original GitHub issueDescribe the bug
When specific text is entered into a TextBox
, I want to enable/disable a ComboBox
based on the TextBox
’s content. For that purpose I created an event
on KeyUp
which calls the VMs function to alter the IsEnable
variable, that’s bound to the ComboBox
’s IsEnabled
property.
The bool flip is ignored by the ComboBox
.
My approach may be faulty but it appeared to make the most sense as commands cannot be bound directly to events yet, from what I could gather.
To Reproduce
- Create a MVVM Project
- MainWindow.axaml:
<Grid ColumnDefinitions="Auto" RowDefinitions="Auto" Margin="4">
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal">
<TextBox Margin="0,20,0,0" Width="300" MaxWidth="300" Height="25" Text="{Binding TextBoxContent, Mode=TwoWay}" Watermark="Enter abc to disable dropdown" KeyUp="OnTextBoxKeyUp"/>
<ComboBox Margin="4,20,0,0" SelectedItem="{Binding SelectedDropDownItem}" Items="{Binding DropDownContent}" IsEnabled="{Binding DropDownIsEnabled}"/>
</StackPanel>
</Grid>
- MainWindow.axaml.cs
private MainWindowViewModel? SpecificViewModel => DataContext as MainWindowViewModel;
private void OnTextBoxKeyUp(object? sender, KeyEventArgs e)
{
SpecificViewModel.OnTextBoxContentChange();
}
- MainWindowViewModel.cs
public MainWindowViewModel()
{
DropDownContent = new ObservableCollectionExtended<string> { "Item1", "Item2", "Item3" };
SelectedDropDownItem = DropDownContent.First();
DropDownIsEnabled = true; //Init with false does disable the ComboBox
}
public string TextBoxContent { get; set; }
public IObservableCollection<string> DropDownContent { get; }
public string SelectedDropDownItem { get; set; }
public bool DropDownIsEnabled { get; private set; }
public void OnTextBoxContentChange()
{
DropDownIsEnabled = TextBoxContent != "abc"; //Y U NO WORK?
}
Expected behavior ComboBox should be disabled if the TextBox Input criteria is fulfilled and (re)enable itself when it is not.
Desktop (please complete the following information):
- OS: Linux; EndeavourOS
- NuGet Version 0.10.17
Additional context Can provide a somewhat minimalist MVVM project, should the code snippets not be sufficient.
Cheers.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
You’re missing SetAndRaiseIfChanged in your view model. Your vm needs to inform the ui if a property changed.
Most of my actual project, not notably larger than the sample but a tad anyway, consists of
Records
andIObservableCollections
, which appear to innately behave as I expect, so this threw me off.For anyone stumbling across this potentially: The backing field with the proposed solution of using
RaiseAndSetIfChanged
can be bypassed by usingReactiveUI.Fody
, which allows to use the[Reactive]
/[ObservableAsProperty]
instead, depending on use case. Under the hood it’ll likely do the same as the proposed solution but keeps the code base a tad cleaner.If a
ReactiveUI
dependency is undesired theINotifyPropertyChanged
route can still be taken, for those interested in it.