WPF:将自定义数据传递到自定义控件和父级之间

时间:2013-10-17 05:14:46

标签: wpf xaml binding

我是WPF的新手。我正在创建我的自定义用户控件,它将显示员工数据结构,如下所示我还实现了INotifyPropertyChanged: 这是代码片段

public class Employee : INotifyPropertyChanged
    {
        private int id;
        private string name;
        private double salary;
        private EmployeeDesignation designation;
        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
                OnPropertyChanged(new PropertyChangedEventArgs("ID"));
                //PropertyChanged(ID, new PropertyChangedEventArgs("ID"));
            }
        }
...
     public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }
    }
}

用户控制代码如下:

<UserControl x:Class="Assignment5.EmployeeUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://scenter code herehemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" DataContext="{Binding RelativeSource={RelativeSource Self}, Path=.}" 
             d:DesignHeight="196" d:DesignWidth="300">
    <Grid Height="182">
        <Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="26,57,0,0" Name="label1" VerticalAlignment="Top" Width="76" />
        <Label Content="Salary" Height="28" HorizontalAlignment="Left" Margin="26,91,0,0" Name="label2" VerticalAlignment="Top" />
        <Label Content="ID" Height="28" HorizontalAlignment="Left" Margin="26,23,0,0" Name="label3" VerticalAlignment="Top" />
        <Label Content="Designation" Height="28" HorizontalAlignment="Left" Margin="26,125,0,0" Name="label4" VerticalAlignment="Top" />
        <TextBox Text="{Binding ElementName=Employee, Path=ID}" Height="23" HorizontalAlignment="Left" Margin="134,28,0,0" Name="IdTextBox" VerticalAlignment="Top" Width="120" />
        <TextBox Text="{Binding ElementName=Employee, Path=Name}" Height="23" HorizontalAlignment="Left" Margin="134,59,0,0" Name="NameTextBox" VerticalAlignment="Top" Width="120" />
        <TextBox Text="{Binding ElementName=Employee, Path=Salary}" Height="23" HorizontalAlignment="Left" Margin="134,91,0,0" Name="SalaryTextBox" VerticalAlignment="Top" Width="120" LostFocus="SalaryTextBoxLostFocus" />
        <TextBox Text="{Binding ElementName=Employee, Path=designation}" Height="23" HorizontalAlignment="Left" Margin="134,125,0,0" Name="DesignationTextBox" VerticalAlignment="Top" Width="120" />
    </Grid>
</UserControl>

这是文件背后的代码

namespace Assignment5
{
    public partial class EmployeeUserControl : UserControl, INotifyPropertyChanged
    {
        public static readonly DependencyProperty EmpDependancyProperty =
                            DependencyProperty.Register("Employee", typeof(Employee), typeof(EmployeeUserControl)
                            , new PropertyMetadata(
                                new PropertyChangedCallback(
                                            EmployeeUserControl.EmployeeDataChanged)));
        public Employee Employee 
        {
            get
            {
                return (Employee)GetValue(EmpDependancyProperty); 
            }
            set
            {
                SetValue(EmpDependancyProperty, value);
            }
        }

        public EmployeeUserControl()
        {
            InitializeComponent();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void SalaryTextBoxLostFocus(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(Employee.Name);
        }
        private static void EmployeeDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            EmployeeUserControl userControl = d as EmployeeUserControl;
            userControl.OnPropertyChanged("Employee");
        }
    }
}

这是我的父母控制......

<Window x:Class="Assignment5.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:my="clr-namespace:Assignment5"
        DataContext="{Binding RelativeSource={RelativeSource Self}, Path=.}">
    <Grid>
        <my:EmployeeUserControl  Employee="{Binding Path=emp, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" HorizontalAlignment="Left" Margin="130,56,0,0" x:Name="employeeUserControl1" VerticalAlignment="Top" Width="285" />
    </Grid>
</Window>

和背后的代码:

namespace Assignment5
{

    public partial class MainWindow : Window
    {
        public static readonly DependencyProperty EmpDependancyProperty =
                        DependencyProperty.Register("Employee", typeof(Employee), typeof(EmployeeUserControl)
                        , new PropertyMetadata(
                            new PropertyChangedCallback(
                                        EmployeeUserControl.EmployeeDataChanged)));
    public Employee Employee 
    {
        get
        {
            return (Employee)GetValue(EmpDependancyProperty); 
        }
        set
        {
            SetValue(EmpDependancyProperty, value);
        }
    }
        public MainWindow()
        {
            emp = new Employee(203,"AAA",23232);
            InitializeComponent();
        }
    }
}

当我运行此代码时。我开始知道父控件中的Employee对象正在初始化,但是在用户控件中它没有被初始化。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

两个问题

  1. UserControl中将Bindings更改为

    <TextBox Text="{Binding Path=Employee.ID}" Height="23" HorizontalAlignment="Left" Margin="134,28,0,0" Name="IdTextBox" VerticalAlignment="Top" Width="120" />
    <TextBox Text="{Binding Employee.Name}" Height="23" HorizontalAlignment="Left" Margin="134,59,0,0" Name="NameTextBox" VerticalAlignment="Top" Width="120" />
    <TextBox Text="{Binding Employee.Salary}" Height="23" HorizontalAlignment="Left" Margin="134,91,0,0" Name="SalaryTextBox" VerticalAlignment="Top" Width="120" LostFocus="SalaryTextBoxLostFocus" />
    <TextBox Text="{Binding Employee.designation}" Height="23" HorizontalAlignment="Left" Margin="134,125,0,0" Name="DesignationTextBox" VerticalAlignment="Top" Width="120" />
    
  2. 在你的窗口中:

    <my:EmployeeUserControl  Employee="{Binding Employee, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" HorizontalAlignment="Left" Margin="130,56,0,0" x:Name="employeeUserControl1" VerticalAlignment="Top" Width="285" />
    

    在构造函数emp = new Employee(203,"AAA",23232);中没有任何意义。而是this.Employee = new Employee(203,"AAA",23232);