次のように、「Employee」というオブジェクトがあります。
class Employee
{
public string EmpName { get; set; }
public string Surname { get; set; }
public Employee(string Name, string Surname) : base()
{
this.EmpName = EmpName;
this.Surname = Surname;
}
public Employee()
{
}
}
この単純な「EmployeeControl」もここにあります。グリッド内の2つのラベル:
<UserControl x:Class="LabelBindingTest.EmployeeCtrl"
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://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Name="EmpCtrl"
d:DesignHeight="120" d:DesignWidth="411">
<Grid>
<Label Content="{Binding ElementName=EmpCtrl, Path=EmpName}" Height="28" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" Width="81" />
<Label Content="{Binding ElementName=EmpCtrl, Path=Surname}" Height="28" HorizontalAlignment="Right" Name="label2" VerticalAlignment="Top" Width="81" />
</Grid>
</UserControl>
(編集)EmployeeControlコードビハインド:
public partial class EmployeeCtrl : UserControl
{
Employee thisEmployee = new Employee();
public string EmpName
{
get
{
return thisEmployee.EmpName;
}
set
{
thisEmployee.EmpName = value;
}
}
public string Surname
{
get
{
return thisEmployee.EmpName;
}
set
{
thisEmployee.EmpName = value;
}
}
public EmployeeCtrl()
{
InitializeComponent();
}
}
今、私がやろうとしているのは、ウィンドウに「EmployeeControl」を追加し、それらをEmployeeオブジェクトにバインドすることです。これが私の「ウィンドウ」コードです。
<Window x:Class="LabelBindingTest.MainWindow"
Name="myWindow"
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:LabelBindingTest">
<Grid>
<my:EmployeeCtrl EmpName="Daniel" Surname="Hammond" HorizontalAlignment="Left" Margin="23,26,0,0" x:Name="employeeCtrl1" VerticalAlignment="Top" Height="97" Width="429" />
</Grid>
</Window>
いろいろ試してみましたが、うまくいきません。正常にコンパイルされますが、ラベルは空です。「Employee」オブジェクトを「EmployeeCtrl」コントロールにアタッチしたいだけです。私がこれをどのように回避するかについてのアイデアはありますか?今日はバインディングについてたくさん読んだのですが、まだ頭を悩ませています。今のところ実行前に従業員名を設定するだけなので、INotifyPropertyChangedを実装する必要があるかどうかはわかりません。
(編集):Caliburn.Microをダウンロードし、最後の回答で同じコードを使用しました。同じ結果、空のウィンドウ。
これがプロジェクト全体です。ただし、この質問にはすべてのコードを貼り付ける必要があります。これは便宜上のものです。