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