在WPF中的XAML中设置DataContext

我有以下代码:

MainWindow.xaml

<Window x:Class="SampleApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" DataContext="{Binding Employee}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="200" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="ID:"/> <Label Grid.Row="1" Grid.Column="0" Content="Name:"/> <TextBox Grid.Column="1" Grid.Row="0" Margin="3" Text="{Binding EmpID}" /> <TextBox Grid.Column="1" Grid.Row="1" Margin="3" Text="{Binding EmpName}" /> </Grid> </Window> 

Employee.cs

 namespace SampleApplication { public class Employee { public Employee() { EmployeeDetails employeeDetails = new EmployeeDetails(); employeeDetails.EmpID = 123; employeeDetails.EmpName = "ABC"; } } public class EmployeeDetails { private int empID; public int EmpID { get { return empID; } set { empID = value; } } private string empName; public string EmpName { get { return empName; } set { empName = value; } } } } 

这是非常简单的代码,我只想将我的Employee.cs类中的EmpIDEmpName属性绑定到MainWindow.xaml中的T​​extboxes的Text属性,但是当我运行代码时,我的这些文本框中没有任何东西出现。 绑定是正确的吗?

这段代码总是会失败。

正如所写的,它说:“在我的DataContext属性上查找名为”Employee“的属性,并将其设置为”DataContext属性“。 显然这是不对的。

为了使您的代码正常工作,请将您的窗口声明更改为:

 <Window x:Class="SampleApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SampleApplication" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <local:Employee/> </Window.DataContext> 

这声明了一个新的XAML名称空间(本地),并将DataContext设置为Employee类的一个实例。 这将导致你的绑定显示默认的数据(从你的构造函数)。

然而,这实际上是你想要的。 相反,你应该有一个新的类(称之为MainViewModel),然后绑定到一个Employee属性,如下所示:

 public class MainViewModel { public Employee MyEmployee { get; set; } //In reality this should utilize INotifyPropertyChanged! } 

现在你的XAML变成:

 <Window x:Class="SampleApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SampleApplication" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <local:MainViewModel/> </Window.DataContext> ... <TextBox Grid.Column="1" Grid.Row="0" Margin="3" Text="{Binding MyEmployee.EmpID}" /> <TextBox Grid.Column="1" Grid.Row="1" Margin="3" Text="{Binding MyEmployee.EmpName}" /> 

现在您可以添加其他属性(其他types,名称)等。有关更多信息,请参阅实现Model-View-ViewModel模式

首先,您应该在Employee类中创build包含员工详细信息的属性:

 public class Employee { public Employee() { EmployeeDetails = new EmployeeDetails(); EmployeeDetails.EmpID = 123; EmployeeDetails.EmpName = "ABC"; } public EmployeeDetails EmployeeDetails { get; set; } } 

如果你不这样做,你将在Employee构造函数中创build对象的实例,并失去对它的引用。

在XAML中,您应该创buildEmployee类的实例,之后您可以将其分配给DataContext

你的XAML应该是这样的:

 <Window x:Class="SampleApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace:SampleApplication" > <Window.Resources> <local:Employee x:Key="Employee" /> </Window.Resources> <Grid DataContext="{StaticResource Employee}"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="200" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="ID:"/> <Label Grid.Row="1" Grid.Column="0" Content="Name:"/> <TextBox Grid.Column="1" Grid.Row="0" Margin="3" Text="{Binding EmployeeDetails.EmpID}" /> <TextBox Grid.Column="1" Grid.Row="1" Margin="3" Text="{Binding EmployeeDetails.EmpName}" /> </Grid> </Window> 

现在,在创build包含员工详细信息的属性之后,您应该使用以下属性进行绑定:

 Text="{Binding EmployeeDetails.EmpID}"