在學習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
恐怕你唯一的行爲就是學習數據綁定的基礎知識並從那裏繼續。 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