WPF - viewing the model update property using a background worker, but the view does not update some elements until it is focused

The view model loads data asynchronously using the background workflow in the model. All properties of the model and presentation model enhance the properties of changed events, and all properties are updated in the view as expected, with the exception of 2 buttons, the state of which IsEnableddepends on the result of loading some properties.

The disgusting part is that as soon as I focus on any part of the view or set a breakpoint after updating the properties (create a delay), then the state of the buttons IsEnabledwill update as expected. Therefore, it seems to be a synchronization problem.

Any tips on how to best solve this? I am using the mvvm-light framework, but that doesn't matter.

I tried binding IsEnabledto a button instead of just relying on a property Command, but that didn't make any difference. I confirmed by registering that the properties of the view model are set, and an event PropertyChangedis generated for the properties associated with the buttons.

Considering sending a message using mvvm-light messenger from a view model to a view on a completed async event, and then somehow? triggering a view update, but this is similar to kludge.

Update

Thanks to the blindmeis answer, I tested the behavior of a button without a set of Command bindings, i.e. just bound the IsEnabled property and works as expected!

<Button 
    Grid.Column="2" Content="{Binding LoadProjectsLabel}"
    VerticalAlignment="Top" HorizontalAlignment="Right" 
    IsEnabled="{Binding CanLoadProjects}" />

, , :), , :

<Button 
    Grid.Column="2" Content="{Binding LoadProjectsLabel}"
    VerticalAlignment="Top" HorizontalAlignment="Right" 
    Command="{Binding LoadProjectsCommand}" />

IsEnabled , .

:

public ICommand LoadProjectsCommand
{
    get
    {
        if (_loadProjectsCommand == null)
        {
            _loadProjectsCommand = new RelayCommand(loadProjects, () => CanLoadProjects);
        }
        return _loadProjectsCommand;
    }            
}

Click Command. , :

<Button 
    Grid.Column="2" Content="{Binding LoadProjectsLabel}"
    VerticalAlignment="Top" HorizontalAlignment="Right" 
    IsEnabled="{Binding CanLoadProjects}" 
    Click="loadProjects_Click"/>

:

void loadProjects_Click(object sender, RoutedEventArgs e)
{
    SettingsViewModel vm = (SettingsViewModel)DataContext;
    vm.LoadProjectsCommand.Execute(null);
}
+3
2

:

BackgroundWorker , CommandManager.InvalidateRequerySposed();

WPF. ICommand "CanExecute". CommandManager .

/ .

EDIT:

, . OnPropertyChanged ( "MyICommand" ) BackgroundWorker .

EDIT:

- .

+4

viewmodel , . , / , .

0

All Articles