1

我是 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 对象正在初始化,但在用户控件中它没有被初始化。我在这里做错了什么?

4

1 回答 1

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);

于 2013-10-17T05:25:31.127 回答