0

我有一个名为“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 并在上一个答案中使用了相同的代码。同样的结果,空的窗口。

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

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

4

2 回答 2

1

好的,首先我强烈建议您考虑 MVVM,因为您的应用程序变得更加复杂,如果您使用的是 MVVM,请使用 MVVM 框架。我会推荐Caliburn.Micro,它可以让你的视图组合变得更容易。还有其他可用的 MVVM 框架,因此请评估它们。

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

INotifyPropetyChanged在使用 MVVM 模式时在视图模型上实现,并在用户控件上实现依赖属性。这两种技术都将通知 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类,因为你没有使用它,但理想情况下,INotifyPropertyChanged如果它的属性值在构造后会发生变化并且你有一个它的实例绑定到 UI 并且你希望它应该实现待更新的用户界面。

于 2013-02-18T23:34:02.223 回答
0

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

于 2013-02-19T07:14:11.357 回答