question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ComboBox ignoring IsEnable binding changes

See original GitHub issue

Describe 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

  1. Create a MVVM Project
  2. 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>
  1. MainWindow.axaml.cs
private MainWindowViewModel? SpecificViewModel => DataContext as MainWindowViewModel;

private void OnTextBoxKeyUp(object? sender, KeyEventArgs e)
{
    SpecificViewModel.OnTextBoxContentChange();
}
  1. 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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
timuniecommented, Jul 30, 2022

You’re missing SetAndRaiseIfChanged in your view model. Your vm needs to inform the ui if a property changed.

0reactions
ClumsyDerpcommented, Jul 30, 2022

Most of my actual project, not notably larger than the sample but a tad anyway, consists of Records and IObservableCollections, 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 using ReactiveUI.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 the INotifyPropertyChanged route can still be taken, for those interested in it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why would multiple IsEnabled bindings fail to work?
I cannot determine why one of them is responding to the binding, but the others are ignoring it. It's also happening with the...
Read more >
WPF ComboBoxEditSettings isEnabled set to false does ...
I want to disable the ComboBoxEditSettings for some of the columns (using a converter or binding the isEnabled to a boolean property) but ......
Read more >
Bound ComboBox within a page and frame not working as ...
Hi, i have a bound ComboBox which i've already posted here. ... ComboBoxItems in the Tapped event and set the IsEnabled property to...
Read more >
Can't get IsEnabled on Item to work in UI for WPF
I've had a standard ComboBox working with this code: < ComboBox. x:Name = "comboWindow1". SelectedValue = "{Binding Window1Page}".
Read more >
How Do I Disable Combobox
If you're using WPF, I recommend binding the IsEnabled property of the 2nd combo box to a property in your view model, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found