2009-11-24 89 views
1

在學習c#和wpf時,我試圖掌握數據綁定。迄今爲止不成功。我在網上找到的所有幫助,即使被描述爲「對於初學者」,對我來說都太複雜了,以至於我無法理解。我將不勝感激,如果有人能在一個很簡單的例子結合提供了代碼:wpf數據綁定新手

namespace BindingTest 
{ 
    class TestClass 
    { 
     public int testProperty; 
     public TestClass() 
     { 
      testProperty = 10; 
     } 
    } 
} 

namespace BindingTest 
{ 
    public partial class Window1 : Window 
    { 
     TestClass iTestClass = new TestClass(); 
     public Window1() 
     { 
      InitializeComponent(); 
     } 
     private void buttonAdd10_Click(object sender, RoutedEventArgs e) 
     { 
      iTestClass.testProperty += 10; 
     } 
    } 
} 

<Window x:Class="BindingTest1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <Grid> 
     <TextBox Height="25" 
       Margin="52,0,130,89" 
       Name="textBox1" 
       VerticalAlignment="Bottom" 
     /> 
     <Button Height="34" 
       HorizontalAlignment="Left" 
       Margin="38,40,0,0" 
       Name="buttonAdd10" 
       VerticalAlignment="Top" 
       Width="62" 
       Click="buttonAdd10_Click"> 
     +10</Button> 
    </Grid>  
</Window> 

所有我想要做的是textBox1.Text結合iTestClass.testProperty,所​​以,當我點擊按鈕,我可以看到它的值在文本框中改變。爲了實現這個簡單的例子,代碼應該做些什麼改變?

如果可以在沒有INotifyPropertyChanged的情況下完成,那就是我想要做到的。 提前謝謝!

Vladimir

+0

恐怕你唯一的行爲就是學習數據綁定的基礎知識並從那裏繼續。 MSDN的WPF數據綁定部分足夠用於啓動(http://msdn.microsoft.com/zh-cn/library/ms750612.aspx)。我還強烈推薦MVVM MSDN雜誌文章(http://msdn.microsoft.com/en-us/magazine/dd419663.aspx)以更好地理解您嘗試繞過的好東西。 – 2009-11-24 13:43:52

回答

3

要綁定到WPF中的屬性,您有兩個選項。

一) INotifyPropertyChanged的
B)的DependencyProperty

既然你不想使用INotifyPropertyChanged,你留下了DependencyProperty

在主窗口的代碼隱藏,在構造中加入這一行:

this.DataContext = iTestClass; 

在主窗口的XAML,該屬性添加到文本框:

Text="{Binding testProperty}" 

變化識別TestClass如下。

class TestClass : DependencyObject 
{ 
    public int testProperty 
    { 
     get { return (int)GetValue(TestPropertyProperty); } 
     set { SetValue(TestPropertyProperty, value); } 
    } 

    public static readonly DependencyProperty TestPropertyProperty = 
     DependencyProperty.Register("testProperty", typeof(int), typeof(TestClass)); 

    public TestClass() 
    { 
     testProperty = 10; 
    } 
} 

如果你喜歡的INotifyPropertyChanged版本,改變的TestClass這個(其餘是相同的):

class TestClass : INotifyPropertyChanged 
{ 
    private int _testProperty; 
    public int testProperty 
    { 
     get { return _testProperty; } 
     set 
     { 
      if (_testProperty == value) 
       return; 

      _testProperty = value; 

      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs("testProperty")); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public TestClass() 
    { 
     testProperty = 10; 
    } 
} 

爲什麼需要這個?除非你以某種方式告訴它,否則WPF無法判斷屬性是否已經改變。

兩種方式WPF可以告訴這是一個事件,它INotifyPropertyChanged提供,或由WPF系統,這是DependencyProperty版本是如何工作的內登記的財產。

如果你不這樣做,那麼WPF將無法知道屬性的值何時發生變化,因此綁定將無法工作。

+0

謝謝,我會研究一下做這件事的依賴屬性的方式,這正是我想到的。 順便說一句,我試着用inotifypropertychanged做這件事,而且我幾乎有同樣的東西,你給我在這裏,但我總是得到錯誤:'BindingTest1.TestClass1'沒有實現接口成員'System.ComponentModel.INotifyPropertyChanged.PropertyChanged',它看起來很奇怪。 Waht可能是這個原因嗎? – butaro 2009-11-24 13:59:23

+0

好吧,忘了錯誤,我找出了我的錯誤。 – butaro 2009-11-24 14:03:18