[英]Binding a WPF control to an Object
我有一個名為“ 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”。 網格中只有兩個標簽:
<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,並在最后一個答案中使用了相同的代碼。 結果相同,窗口為空。
這是整個項目。 不過,所有代碼都應粘貼在此問題中。 這只是為了方便起見。
好的,首先,我強烈建議您考慮MVVM,因為您的應用程序變得越來越復雜, 如果您使用的是MVVM ,請使用MVVM框架 。 我建議使用Caliburn.Micro ,它使您的視圖合成變得容易得多。 還有其他可用的MVVM框架,因此請對其進行評估。
對於您的問題,問題在於您的用戶控件未將其屬性實現為依賴項屬性。 在XAML中,您正在設置用戶控件的EmpName
和Surname
屬性,但是UI在構建用戶控件后不知道它們的值已更改,因此不會自我更新。
使用MVVM模式時,可以在視圖模型上實現INotifyPropetyChanged
,並在用戶控件上實現依賴項屬性 。 這兩種技術都會將更改通知UI,並迫使其自行更新。
因此,對於您的用戶控件,您可以執行以下操作:
public partial class EmployeeCtrl : UserControl
{
public static readonly DependencyProperty EmpNameProperty =
DependencyProperty.Register("EmpName", typeof(string),
typeof(EmployeeCtrl), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty SurnameProperty =
DependencyProperty.Register("Surname", typeof(string),
typeof(EmployeeCtrl), new FrameworkPropertyMetadata(null));
public EmployeeCtrl()
{
this.InitializeComponent();
}
public string EmpName
{
get { return (string)GetValue(EmpNameProperty); }
set { SetValue(EmpNameProperty, value); }
}
public string Surname
{
get { return (string)GetValue(SurnameProperty); }
set { SetValue(SurnameProperty, value); }
}
}
實際上,在此階段您根本不需要使用Employee
類,因為您沒有使用它,但是理想情況下,如果它的屬性值在構造后會更改並且您有一個實例綁定到UI,並且它應該實現INotifyPropertyChanged
希望更新用戶界面。
您需要在EmployeeCtrl的EmpName和Surname屬性上實現INotifyPropertyChanged,因為此用戶控件中的Label需要更新此屬性值的值更改,以便Label可以更新其值。 因此,您必須對EmployeeCtrl的EmpName和Surname屬性實施Notify更改,或僅對這兩個屬性使用依賴項屬性。 希望這可以幫助!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.