1.新建person类文件,定义两个属性:Name,Address
public class Person
{
public string Name { get; set; }
public string Address { get; set; }
}
2.在前台MainPage.xaml文件里定义资源文件
<UserControl.Resources>
<model:Person x:Key="test" Name="kelei" Address="湖北" />
</UserControl.Resources>
3.对控件文本值进行数据绑定
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource test}">
<TextBlock Height="23" HorizontalAlignment="Left" Margin="153,114,0,0" Name="textBlock1" Text="{Binding Path=Name}" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" TextAlignment="Center" Margin="110,154,0,0" Name="textBox1" Text="{Binding Path=Address}" VerticalAlignment="Top" Width="120" />
</Grid>
{Binding Name}个人省略了Path属性,{Binding Name}={Binding Path=Name}
总结:上述方法只是简单的数据绑定,不涉及动态数据绑定。动态数据绑定需要继承接口INotifyPropertyChanged
{Binding RelativeSource={RelativeSource Self}, Path=Name} 可以用这个方法binding自身的属性,以前有人告诉我非要是依赖属性才能绑定,后来自己试验了一下,发现一般属性也是可以进行绑定的(PS:这个是无聊的题外话)