简体   繁体   中英

Combobox value changes, but visually SelectedValue stays the same

I have a combobox with a custom enum (just true/false). I have a function that checks conditions if the SelectedValue changes from false to true and if the conditions are wrong it changes the combobox SelectedValue back to false. This changes the SelectedValue to false if you check it in code, but when you look at the UI it's still on true.

Here's the xaml for the combobox:

<ComboBox x:Name="comboEnabled1" Width="80" Height="26"
                          ItemsSource="{Binding Path=TrueFalseChoices}"
                          SelectedValue="{Binding Path=Enable1, Mode=TwoWay}"/>
            

Here's the viewmodel

private TrueFalse _enable1 = TrueFalse.False;
    public TrueFalse Enable1
    {
        get { return _enable1; }
        set
        {
            if (_enable1 != value)
            {
                _enable1 = value;
                base.OnPropertyChanged("Enable1");
                OnEnableChanged(EventArgs.Empty);
            }
        }
    }

And here's the function that I'm using to check the conditions

public void HandleEnable(object sender, EventArgs e)
    {
        if(Enable1 == TrueFalse.True)
        {
            if(!connected)
            {
                HandleMessage("Can't enable, not connected");
                Enable1 = TrueFalse.False;
            }
            else if (!_main.CBCheck(_main.cbReason))
            {
                Enable1 = TrueFalse.False;
            }

        }
        Console.WriteLine("Enabled {0}", Enable1);

    }

Was thinking I'm changing the value too rapidly, but the last Console.Writeline produces the right outcome each time.

Any help appreciated!

Edit: Calling Handleenable here:

protected void OnEnableChanged(EventArgs e)
    {
        EventHandler handler = EnableChanged;
        if (handler != null)
            handler(this, e);
    }

And in the ViewModel funct:

        EnableChanged += HandleEnable;

Changing the Enable1 in any other place worked as it should have, only having issues in HandleEnable function.Also tried changing other comboboxes in the HandleEnable function and that worked as it should have.

I would recommend actually disabling the ComboBox if the requirements are not met.

But if you insist on reverting Enable1 back to False if conditions are not met, you should push the notification properly through the dispatcher.

set
{
    var effectiveValue = condition ? value : TrueFalse.False;
    if (effectiveValue == TrueFalse.False && value == TrueFalse.True)
        System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(
            new Action(() => base.OnPropertyChanged("Enable1"), null));
    //your regular set-code follows here
}

It happens because WPF is already responding to that event, and therefore ignoring the subsequent calls until it's done. So you immediately queue another pass as soon as the current one is finished.

But I would still recommend disabling the ComboBox when it is effectively disabled. Accessing the dispatcher from a viewmodel does not smell good no matter how you look at it.

UPD: You can also solve that with {Binding Enable1, Delay=10} if your framework is 4.5.1+.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM