WPF xaml:无法使用local绑定数据

时间:2016-03-08 13:40:57

标签: c# wpf xaml

我关注This Data Binding Tutorial。在示例03中,它们显示以下代码,

<!-- Tutorial CODE -->

<Window.Resources>
     <local:Employee 
        x:Key="MyEmployee" EmployeeNumber="123" FirstName="John" 
       LastName="Doe" Department="Product Development" Title="QA Manager" 
    />
</Window.Resources>
<Grid DataContext="{StaticResource MyEmployee}">
    <TextBox Text="{Binding Path=EmployeeNumber}"></TextBox>
    <TextBox Text="{Binding Path=FirstName}"></TextBox>
    <TextBox Text="{Binding Path=LastName}" />
    <TextBox Text="{Binding Path=Title}"></TextBox>
    <TextBox Text="{Binding Path=Department}" />
</Grid>

我添加了其他基本标记,例如windows,并实现了如下代码

<!-- My CODE -->

<Window x:Class="Data_Binding_Example_03.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Data_Binding_Example_03"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:Employee 
                x:Key="MyEmployee"
                EmployeeNumber="123"
                FirstName="John"
                LastName="Doe"
                Department="Product Development"
                Title="QA Manager"
        />
    </Window.Resources>
    <Grid DataContext="{StaticResource MyEmployee}">
        <TextBox Text="{Binding Path=EmployeeNumber}"></TextBox>
        <TextBox Text="{Binding Path=FirstName}"></TextBox>
    </Grid>
</Window>

但我收到错误

enter image description here

我该怎么办?

1 个答案:

答案 0 :(得分:4)

您必须使用以下内容创建一个Employee类(Employee.cs文件):

public class Employee {
    public int EmployeeNumber {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public string Department {get;set;}
    public string Title {get;set;}
}