0
XAML:TextBlock的UI不是INotifyPropertyChanged的更新
<Button Content="Refresh" Margin="0,5,0,0" Width="120" Click="RefreshPlayers"/>
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<StackPanel.Resources>
<viewModels:LastRefreshedViewModel x:Key="LastRefreshedViewModel"/>
</StackPanel.Resources>
<TextBlock Text="Last Refreshed:" HorizontalAlignment="Center"/>
<TextBlock DataContext="{StaticResource LastRefreshedViewModel}" Text="{Binding LastRefreshTimeString}" Margin="3,0,0,0"/>
</StackPanel>
主窗口:
private void RefreshPlayers(object sender, RoutedEventArgs e)
{
RefreshPlayers();
}
private void RefreshPlayers()
{
GetSteamInfo();
_lastRefreshedViewModel.Reset();
}
視圖模型:
public class LastRefreshedViewModel : PropertyChangedBase
{
private int _lastRefreshTime;
public string LastRefreshTimeString
{
get {
return _lastRefreshTime == 0 ? "Never" : _lastRefreshTime + " min";
}
}
public int LastRefreshTime
{
get { return _lastRefreshTime; }
set {
{
if (value == _lastRefreshTime) {
return;
}
_lastRefreshTime = value;
NotifyOfPropertyChange(() => LastRefreshTimeString);
NotifyOfPropertyChange(() => LastRefreshTime);
}}
}
public LastRefreshedViewModel()
{
_lastRefreshTime = 0;
}
public void Update()
{
LastRefreshTime++;
}
public void Reset()
{
LastRefreshTime = 1;
}
每當我按 「刷新」,在LastRefreshTime更新,但文本塊用戶界面不。我懷疑這是一個線程問題,但是我嘗試使用backgroundworkers,線程池和調度程序失敗。
我現在看到的文字塊的DataContext的本身並不_lastRefreshedViewModel,謝謝 – user2511283