2016-11-20 41 views
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,線程池和調度程序失敗。

回答

0

您應該爲任何控件的父級設置datacontext(在您的案例textblock中)。 例如。

XAML:

<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"/> 
     <Grid x:Name="myGrid" DataContext="{StaticResource LastRefreshedViewModel}"> 
      <TextBlock Text="{Binding LastRefreshTimeString}" Margin="3,0,0,0"/> 
     </Grid> 
    </StackPanel> 

代碼背後:

private void RefreshPlayers(object sender, RoutedEventArgs e) 
    {    
     if(myGrid.DataContext != null) 
     { 
      var viewModel = myGrid.DataContext as LastRefreshedViewModel; 
      if(viewModel != null) 
      { 
       viewModel.Reset(); 
      } 
     } 
    } 

視圖模型:

public class LastRefreshedViewModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged(String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    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; 
       NotifyPropertyChanged("LastRefreshTimeString"); 
       NotifyPropertyChanged("LastRefreshTime"); 
      } 
     } 
    } 

    public LastRefreshedViewModel() 
    { 
     _lastRefreshTime = 0; 
    } 

    public void Update() 
    { 
     LastRefreshTime++; 
    } 

    public void Reset() 
    { 
     LastRefreshTime = 1; 
    } 
} 
+0

我現在看到的文字塊的DataContext的本身並不_lastRefreshedViewModel,謝謝 – user2511283