2010-09-08 77 views
3

我附上了一些WPF C#綁定代碼 - 爲什麼這個簡單的例子不工作? (只是想了解綁定到自定義對象)。這是當點擊按鈕增加模型中的計數器時,標籤不會更新。WPF C#綁定代碼 - 爲什麼這個簡單的例子不工作?

<Window x:Class="testapp1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Button Height="23" HorizontalAlignment="Left" Margin="20,12,0,0" 
       Name="testButton" VerticalAlignment="Top" Width="126" 
       Click="testButton_Click" Content="Increase Counter" /> 
     <Label Content="{Binding Path=TestCounter}" Height="37" 
       HorizontalAlignment="Right" Margin="0,12,122,0" 
       Name="testLabel2" VerticalAlignment="Top" 
       BorderThickness="3" MinWidth="200" /> 
    </Grid> 
</Window> 


namespace testapp1 
{ 
    public partial class MainWindow : Window 
    { 
     public TestModel _model; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      InitializeComponent(); 
      _model = new TestModel(); 
      _model.TestCounter = 0; 
      this.DataContext = _model; 
     } 

     private void testButton_Click(object sender, RoutedEventArgs e) 
     { 
      _model.TestCounter = _model.TestCounter + 1; 
      Debug.WriteLine("TestCounter = " + _model.TestCounter); 
     } 
    } 

    public class TestModel : DependencyObject 
    { 
     public int TestCounter { get; set; } 
    } 

} 

感謝

回答

3

TestCounter需要一個DepenencyProperty

public int TestCounter 
    { 
     get { return (int)GetValue(TestCounterProperty); } 
     set { SetValue(TestCounterProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for TestCounter. 
    //This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty TestCounterProperty = 
     DependencyProperty.Register 
      ("TestCounter", 
       typeof(int), 
       typeof(TestModel), 
       new UIPropertyMetadata(0)); 
+0

謝謝 - 現在就試試這個 - 所以你永遠不能直接綁定到一個普通的舊類對象,然後在WPF中? – Greg 2010-09-08 06:01:39

+0

如果您實現INotifyPropertyChanged – Daniel 2010-09-08 06:02:29

+0

,那麼您可以不,完全沒有 - 只要您通知目標(例如Textbox上的TextProperty)任何更改,就可以綁定到任何POCO對象的任何屬性。 DependenyProperties爲您完成這項工作,但您也可以在TestModel類上實現INotifyPropertyChanged(根據@ rudigrobler建議)並手動引發更改事件... – kiwipom 2010-09-08 06:04:32

4

對於這個簡單的例子,可以考慮使用INotifyPropertyChanged的,而不是DependencyProperties!

UPDATE 如果你想使用DPS,使用VS2010或Dr WPF's snippets for VS2008的propdp片段?

+0

+1因爲你被重寫爲'考慮使用';) – kiwipom 2010-09-08 06:05:47

1

您可以在System.ComponentModel名稱空間中實現INotifyPropertyChanged接口。我通常實現一個Changed方法,該方法可以獲取多個屬性名稱並檢查未設置的事件。我這樣做是因爲有時我有多個屬性取決於一個值,我可以從所有屬性設置器中調用一個方法。例如,如果您有一個帶寬度和高度屬性的Rectangle類和一個返回寬度*高度的Area只讀屬性,則可以將Changed(「Width」,「Area」);在寬度屬性設置器。

public class TestModel : INotifyPropertyChanged 
{ 
    int m_TestCounter; 
    public int TestCounter { 
     get { 
      return m_TestCounter; 
     } 
     set { 
      m_TestCounter = value; 
      Changed("TestCounter"); 
     } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 

    void Changed(params string[] propertyNames) 
    { 
     if (PropertyChanged != null) 
     { 
      foreach (string propertyName in propertyNames) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 
+0

Jason - 試圖理解這個建議如何應用於我遇到的問題是什麼?你如何在這裏使用這個概念? – Greg 2010-09-13 01:08:10

+0

當您的對象綁定到控件時,控件將自己添加爲來自INotifyPropertyChanged接口的PropertyChanged事件的偵聽器。當您在屬性設置器中觸發該事件時,綁定到該屬性的控件將在UI中更新。上面的代碼會在調用TestCounter設置器時自動更新UI。你需要在屬性後面有一個變量,並在你的setter中調用Changed方法,但是比使用DependencyProperty更容易。 – 2010-09-14 20:39:57

相關問題