Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I update WPF StatusBar text before making the user wait?
    primarykey
    data
    text
    <p>I've got a WPF application with a status bar.</p> <pre><code>&lt;StatusBar Grid.Row="1" Height="23" Name="StatusBar1" VerticalAlignment="Bottom"&gt; &lt;TextBlock Name="TextBlockStatus" /&gt; &lt;/StatusBar&gt; </code></pre> <p>I'd like to display text there and switch to the hourglass Wait cursor when I do a small amount of work.</p> <p>This code will update the cursor, but the StatusBar <strong>text does not update</strong>...</p> <pre><code>Cursor = Cursors.Wait TextBlockStatus.Text = "Loading..." System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3)) TextBlockStatus.Text = String.Empty Cursor = Cursors.Arrow </code></pre> <hr> <h2>Update</h2> <p>Inspired by <a href="https://stackoverflow.com/users/77255/alexandra">Alexandra</a>'s <a href="https://stackoverflow.com/questions/782359/can-i-update-wpf-statusbar-text-before-making-the-user-wait/782429#782429">answer</a>...</p> <p><strong>It works</strong> if I do it this way, but I'm not at all happy with this solution. Is there a simpler way?</p> <pre><code>Delegate Sub Load1() Sub Load2() System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3)) End Sub Dim Load3 As Load1 = AddressOf Load2 Sub Load() Cursor = Cursors.Wait TextBlockStatus.Text = "Loading..." Dispatcher.Invoke(DispatcherPriority.Background, Load3) TextBlockStatus.Text = String.Empty Cursor = Cursors.Arrow End Sub </code></pre> <p>I'd rather it instead looked something like this...</p> <pre><code>Sub Load() Cursor = Cursors.Wait TextBlockStatus.Text = "Loading..." 'somehow put all the Dispatcher, Invoke, Delegate, AddressOf, and method definition stuff here' TextBlockStatus.Text = String.Empty Cursor = Cursors.Arrow End Sub </code></pre> <p>Or even better...</p> <pre><code>Sub Load() Cursor = Cursors.Wait ForceStatus("Loading...") System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3)) ForceStatus(String.Empty) Cursor = Cursors.Arrow End Sub Sub ForceStatus(ByVal Text As String) TextBlockStatus.Text = Text 'perform magic' End Sub </code></pre> <hr> <h2>Update</h2> <p>I've also tried to bind the TextBlock to a public property and implement <a href="http://msdn.microsoft.com/en-us/library/ms743695.aspx" rel="nofollow noreferrer">INotifyPropertyChanged</a> as <a href="https://stackoverflow.com/users/94159/iangilham">IanGilham</a> <a href="https://stackoverflow.com/questions/782359/can-i-update-wpf-statusbar-text-before-making-the-user-wait/782946#782946">suggested</a>. This <strong>does not work</strong>.</p> <p>XAML:</p> <pre><code>&lt;TextBlock Text="{Binding Path=StatusText}"/&gt; </code></pre> <p>Visual Basic:</p> <pre><code>Imports System.ComponentModel Partial Public Class Window1 Implements INotifyPropertyChanged Private _StatusText As String = String.Empty Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Property StatusText() As String Get Return _StatusText End Get Set(ByVal value As String) _StatusText = value OnPropertyChanged("StatusText") End Set End Property Shadows Sub OnPropertyChanged(ByVal name As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name)) End Sub ... Sub Load() ... Cursor = Cursors.Wait StatusText = "Loading..." System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3)) StatusText = String.Empty Cursor = Cursors.Arrow ... End Sub ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload