ステータスバー付きの WPF アプリケーションがあります。
<StatusBar Grid.Row="1"
Height="23"
Name="StatusBar1"
VerticalAlignment="Bottom">
<TextBlock Name="TextBlockStatus" />
</StatusBar>
そこにテキストを表示して、ちょっとした作業をするときは砂時計の待機カーソルに切り替えたいです。
このコードはカーソルを更新しますが、StatusBar のテキストは更新されません...
Cursor = Cursors.Wait
TextBlockStatus.Text = "Loading..."
System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3))
TextBlockStatus.Text = String.Empty
Cursor = Cursors.Arrow
アップデート
このようにすればうまくいきますが、この解決策にはまったく満足していません。もっと簡単な方法はありますか?
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
むしろこんな感じでよかったのに…
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
またはさらに良い...
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
アップデート
また、TextBlock をパブリック プロパティにバインドし、IanGilhamが提案したようにINotifyPropertyChangedを実装しようとしました。これは機能しません。
XAML:
<TextBlock Text="{Binding Path=StatusText}"/>
ビジュアルベーシック:
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
...