Domanda

I have a simple button:

<Button Content="Login" Command="{Binding StartAction}" IsEnabled="{Binding Path=Loading, Converter={StaticResource InverseBooleanConverter}}"/>

As you can see, IsEnabled is bound to a boolean property. When StartAction is executed, the boolean is set on true so the button gets disabled (note that I'm using a InverseBooleanConverter). This works fine.

After changing the boolean, the StartAction-method creates a new thread which calls a callback on the ViewModel (where the thread was created, so where the StartAction-method can be found) when it's finished. This callback is very simple:

private void DisplayResult()
{
    Loading = false;
}

This should enable the button again, however, this doesn't happen.

If I set loading on false somewhere else, the button DOES get enabled. Together with the button, some other elements (also bound to this boolean) should get invisible/visible when the boolean changes and that DOES work fine too, even when the Loading property is changed by the callback method.

I'm a bit confused here. Why doesn't it work? I think it may have something to do with the thread, but I don't see any reason why this shouldn't work, especially since it works with other elements on the UI.

Just to be clear, this is the Loading property:

public bool Loading
{
    get { return loading; }
    set
    {
        if (loading != value)
        {
            loading = value;
            RaisePropertyChanged("Loading");
        }
    }
}

Any idea what might be wrong here?

Thanks

È stato utile?

Soluzione

On your callback you may need to instruct the UI to check that property again and update accordingly. This is performed with the CommandManager. I've added a bit to your callback code:

private void DisplayResult()
{
    Loading = false;
    CommandManager.InvalidateRequerySuggested();
}

The CommandManager.InvalidateRequerySuggested instructs WPF to update any bound controls as necessary right away.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top