1、
XAML:<TextBox Name="textbox1"></TextBox>
cs:
public class Customer
{
public string Name { get;set;}
}
Customer customer = new Customer()
{
Name = "Zhangjinshan"
};
Binding bind = new Binding();
bind.Source = customer;
bind.Path = new PropertyPath("Name");
textbox1.SetBinding(TextBox.TextProperty, bind);
2、
XAML: <TextBox Name="textbox1" Text="{Binding Path=Name}"></TextBox>
cs:
Customer customer = new Customer()
{
Name = "Zhangjinshan"
};
textbox1.DataContext = customer;
3、
XAML:
<Window.Resources>
<my:Customer x:Key="mycustomer" Name="ZhangJinshan"/>
</Window.Resources>
<StackPanel>
<StackPanel.DataContext>
<StaticResource ResourceKey="mycustomer"/>
</StackPanel.DataContext>
<TextBox Name="textbox1" Text="{Binding Path=Name}"/>
</StackPanel>
4、
<Window.Resources>
<my:Customer x:Key="mycustomer" Name="Zhangjinshan"/>
</Window.Resources>
<StackPanel>
<TextBox Name="textbox1" Text="{Binding Path=Name, Source={StaticResource ResourceKey=mycustomer}}"/>
</StackPanel>
有一个问题,就是很多地方都写可以这样绑定:
<TextBox DataContext="{Binding ElementName=customer}" Text="{Binding Path=Name}"/>
可我怎么试都不行,可能是我理解有误:代码如下:
XAML:
<TextBox DataContext="{Binding ElementName=customer}" Text="{Binding Path=Name}"/>
cs:
Customer customer = new Customer()
{
Name = "Zhangjinshan"
};
请高人指点。。。。