读书人

WPF相干xaml中Grid.DataContext绑定为

发布时间: 2013-03-17 13:48:32 作者: rapoo

WPF有关xaml中Grid.DataContext绑定为null,求解???
WPF有关xaml中Grid.DataContext绑定为null,求解???、
xaml中:
<Grid Name="gridEmployee">
<TextBox Text="{Binding Name}" />
<TextBox Text="{Binding Number}" />
<Button Content="Button" Name="button1" Click="button1_Click" />
</Grid>

Model中:
public class Employee
{
public string Name { get; set; }
public string Number { get; set; }
}

代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
Employee emp = new Employee();
emp = (Employee)gridEmployee.DataContext;
MessageBox.Show(emp.Name);
}
运行后,单击button怎么提示”未将对象引用设置到对象的实例。“,而且emp为null,求解????? wpf null button textbox binding
[解决办法]

引用:
绑定了, <TextBox Text="{Binding Name} Name="txtName">

你那是 绑定模版 绑定的数据源呢? 你在哪绑定的数据源
[解决办法]
你得在你的XAML中绑定一下或者后台代码中绑定一下。
在后台的构造函数中加上This.DataContext = new Employee();

读书人网 >C#

热点推荐