今天试着用data binding写了个小程序, 发现对c#的基本概念还是不清楚,导致走了弯路
window 1.xaml
<Window x:Class="bindtest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300"
xmlns:localadd ="clr-namespace:bindtest"
>
<DockPanel>
<DockPanel.Resources>
<localadd:MyData x:Key="vaav" />
</DockPanel.Resources>
<DockPanel.DataContext>
<Binding Source="{StaticResource vaav}"/>
</DockPanel.DataContext>
<Button Click="ClickMe" Background="{Binding Path=colorName }" Width="150" Height="30">
I am bound to be RED!
</Button>
</DockPanel>
</Window>
MyData.cs
using System;
namespace bindtest
{
public class MyData
{
private string ColorName = "Red";
public string colorName
{
get { return ColorName; }
set { ColorName = value; }
}
}
}
文档中说Path=colorName , colorName是属性字段, 就是对属性这个基本概念的不清,导致我写成了
using System;
namespace bindtest
{
public class MyData
{
public string colorName = "Red"; //Error code
}
}