2016-04-15 261 views
4

我在更改整數字段時更新簡單數據綁定標籤時遇到問題。我實現了INotifyPropertChanged,當我改變我的變量值時,這個事件被觸發。用戶界面不會更新,標籤也不會更改。過去我沒有做過太多的數據綁定,所以我可能錯過了一些東西,但我還沒有找到它。XAML數據綁定在屬性更改時不更新UI

這裏是我有我的XAML:

  <Window x:Class="TestBinding.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
       xmlns:mc="http://schemas.openxmlformats.org/markup- 
       compatibility/2006" 
       xmlns:local="clr-namespace:TestBinding" 
       mc:Ignorable="d" 
       Title="MainWindow" Height="350" Width="525"> 
      <StackPanel> 
       <Button Command="{Binding TestCommand, Mode=OneWay}" > 
        <Button.DataContext> 
         <local:TestVM/> 
        </Button.DataContext> Add 1</Button> 
       <Label Content="{Binding Count, 
       Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"> 
        <Label.DataContext> 
         <local:TestVM/> 
        </Label.DataContext> 
       </Label> 
      </StackPanel> 
     </Window> 

這裏是我的視圖模型C#:

class TestVM : INotifyPropertyChanged 
    { 
     private int _count; 
     private RelayCommand _testCommand; 

     public TestVM() 
     { 
      Count=0; 

     } 

     public int Count 
     { 
      get { return _count; } 
      set { _count = value; OnPropertyChanged(); } 
     } 

     public void Test() 
     { 
      Count++; 
     } 

     public ICommand TestCommand 
     { 
      get 
      { 
       if (_testCommand == null) 
       { 
        _testCommand = new RelayCommand(param => this.Test(), param => true); 
       } 

       return _testCommand; 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") 
     { 
      if (this.PropertyChanged != null) 
      { 
       Console.WriteLine(propertyName); 
       var e = new PropertyChangedEventArgs(propertyName); 
       this.PropertyChanged(this, e); 
      } 
     } 
    } 

這裏是我的ICommand C#(如果你需要它來複制我有什麼):

public class RelayCommand : ICommand 
    { 
     #region Fields 
     readonly Action<object> _execute; 
     readonly Predicate<object> _canExecute; 
     #endregion // Fields 

     #region Constructors 
     public RelayCommand(Action<object> execute) : this(execute, null) { } 
     public RelayCommand(Action<object> execute, Predicate<object> canExecute) 
     { 
      if (execute == null) throw new ArgumentNullException("execute"); 
      _execute = execute; 
      _canExecute = canExecute; 
     } 
     #endregion // Constructors 

     #region ICommand Members [DebuggerStepThrough] 
     public bool CanExecute(object parameter) 
     { 
      return _canExecute == null ? true : _canExecute(parameter); 
     } 

     public event EventHandler CanExecuteChanged 
     { 
      add { CommandManager.RequerySuggested += value; } 
      remove { CommandManager.RequerySuggested -= value; } 
     } 

     public void Execute(object parameter) 
     { 
      _execute(parameter); 
     } 
     #endregion 
     // ICommand Members 
    } 

任何和所有的幫助,非常感謝。

編輯: 我更新了我的xaml toadflakz建議和它的工作。

<Window x:Class="TestBinding.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:TestBinding" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
     <local:TestVM/> 
    </Window.DataContext> 
    <StackPanel> 
    <Button Command="{Binding TestCommand, Mode=OneWay}" >Add 1</Button> 
    <Label Content="{Binding Count, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" /> 
</StackPanel> 
</Window> 

回答

9

你的問題是你將DataContext綁定到單獨控件的方式。你的ViewModel不是自動的Singleton(單實例對象),所以每次你指定它時,你實際上是在創建一個單獨的實例。

如果您在Window級別設置DataContext,那麼您的代碼應該按預期工作。

4

設置DataContextWindow爲:

<Window...... 
    <Window.DataContext> 
     <local:TestVM/> 
    </Window.DataContext> 
</Window> 

您已設置爲DataContextButton分別Label,所以會有的TestVM class兩個不同的對象。 Command將首先執行並更改其中的值, 而您的Label將顯示來自另一個對象的值。

相關問題