WPF Gui is updated with a different theme

I have a Gui popup with a team binding,

<Grid x:Name="popup" Visibility="Hidden" DataContext="{Binding Path=PopupMsg}" > <TextBlock x:Name="tbMessage" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Margin="20,70,10,0" Text="{Binding Path=Message}" FontSize="16"/> <Button x:Name="btnPopupOk" Grid.Row="1" Grid.Column="2" Content="{Binding Path=OkContent}" Margin="10,40,10,10" Command="{Binding}" CommandParameter="true" /> </Grid> </Border> </Grid> 

in a C # file, I link the command:

  CommandBinding okCommandBinding = new CommandBinding(OkCommand); okCommandBinding.Executed += popupButtons_Executed; okCommandBinding.CanExecute += okCommandBinding_CanExecute; CommandBindings.Add(okCommandBinding); btnPopupOk.Command = OkCommand; 

It works fine when I use it from the same topic, when I get a callback from a web service that is in a different thread, I use a dispatcher to display a message, I can see new text in a popup, but the binding isn’t the working button remains unavailable (CanExecute = false). When I click on the screen with the mouse, the popup updates the actual CanExecute value and a button appears.

 System.Windows.Threading.DispatcherPriority.Normal, new Action( delegate() { popup.Visibility = Visibility.Visible; popup.Focus(); })); 
+4
source share
3 answers

this is a piece of code that I use to fix any cross-thread calls when updating the WPF interface.

  this.Dispatcher.BeginInvoke( (Action)delegate() { //Update code goes in here }); 

Hope this helps

+12
source

Your problem is not in streaming, but in causing the command to call CanExecute .

Usually, only certain gui events will trigger redirected commands, and since WPF does not know to call CanExecute when data changes, this is not the case.

To manually force all redirected commands to be updated, call CommandManager.InvalidateRequerySuggested . If the command is based on a message, which I would call when the message was modified.

+2
source

You need to use the dispatcher to get the visibility update to go through the main GUI thread (for example, you need to use Invoke with WinForms)

See the MSDN Forums for more information.

Basically something like:

  popup.Dispatcher.Invoke(DispatcherPriority.Normal, delegate() { popup.Visibilty = Visibility.Visible; popup.Focus(); }); 
+1
source

All Articles