2013-02-18 128 views
0

我有一个名为“雇员”的对象,如下所示:绑定WPF控件对象

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” s到我的窗口,并结合他们的员工对象。 这里是我的“窗口”,代码:

<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,并在最后一个答案中使用了相同的代码。相同的结果,空的窗口。

这是整个项目。虽然它的所有代码应该粘贴在这个问题中。这只是为了方便。

http://www.mediafire.com/?gux3573rz64mupe

+0

你可以显示用户控制的代码吗?\ – AbZy 2013-02-18 23:33:43

+0

你在MainWindow中设置了什么DataContext? – 2013-02-18 23:37:54

回答

1

好吧,首先,我会强烈建议考虑MVVM为您的应用程序变得更加复杂,而use an MVVM framework if you're using MVVM。我会推荐Caliburn.Micro这使得您的视图组合更容易。还有其他MVVM框架可用,因此请评估它们。

对于你的问题,问题是你的用户控件没有实现它的属性作为依赖属性。在XAML中,您正在设置用户控件的EmpNameSurname属性,但UI在构造用户控件后并不知道它们的值已更改,因此不会自行更新。

您在使用MVVM模式的视图模型和用户控件的dependency properties上实现了INotifyPropetyChanged。这两种技术都会通知用户界面更改并强制更新。

因此,对于你的用户控件,你可以这样做:

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类都在这个阶段,你不使用它,但最好应该落实INotifyPropertyChanged如果是属性值将在构建后发生变化,并且您有一个实例绑定到UI,并且希望UI被更新。

+0

请参阅编辑。不过,我现在不会在这里待上几个小时。 – Harry 2013-02-19 00:18:21

+0

答复已更新。 – devdigital 2013-02-19 08:01:09

+0

它仍然不起作用。也许在UserControl XAML中绑定有问题? '

0

您需要在EmployeeCtrl的EmpName和Surname属性上实现INotifyPropertyChanged,因为此用户控件中的标签需要更新此属性值中的值更改,以便标签可以更新其值。因此,您必须在EmployeeCtrl的EmpName和Surname属性上实现通知更改,或者只需对此两个属性使用依赖项属性。希望这可以帮助!!!