简体   繁体   English

使用Microsoft Expression Blend将CLR对象绑定到WPF控件

[英]Bind CLR object to WPF control using Microsoft Expression Blend

I have the following class Person: 我有以下课程的Person:

public class Person
{
     public string Name
     {
          get { return name; }
          set { name = value; }
     }

     public string Nickname
     {
          get { return nickname; }
          set { nickname = value; }
     }

     private string nickname;
     private string name; 

     public Person(DataRow row)
     {
          this.name = Convert.ToString(row["Name"]);
          this.nickname = Convert.ToString(row["Nickname"]);
     }
}

and another class PersonEditorFormController in the different assembly, that acts like the engine for WPF class PersonEditorForm.xaml: 和另一个程序集中的另一个类PersonEditorFormController,其行为类似于WPF类PersonEditorForm.xaml的引擎:

public class PersonEditorFormController
{
    public Person SelectedPerson
    {
        get { return person; }
        set { person = value; }
    }

    private Person person;

    public void GetPerson(string name)
    {
        try
        {
            Common.dbController.OpenConnection();
            Common.dbController.BeginTransaction();

            PersonController personController= new PersonController();
            string[] fields = new string[] { "Name" };
            string[] values = new string[] { name };
            this.person = personController.GetPerson(fields, values);

            Common.dbController.CommitTransaction();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            Common.dbController.CloseConnection();
        }
    }
}

PersonController is a class that provides the means to get the data from the database and to construct the person object. PersonController是一个类,提供了从数据库获取数据并构造person对象的方法。

Now, PersonEditorForm.xaml has one text box called NicknameTextBox. 现在,PersonEditorForm.xaml具有一个名为NicknameTextBox的文本框。 I want to bind the nickname of the person to its Text property using Microsoft Expression Blend. 我想使用Microsoft Expression Blend将人的昵称绑定到其Text属性。 How do I do that? 我怎么做?

Here's what I tried so far (with no success): 这是我到目前为止尝试过的(没有成功):

  1. I created new Object Data Source to LayoutRoot that points to my engine class, ie PersonEditorFormController. 我为LayoutRoot创建了新的对象数据源,它指向我的引擎类,即PersonEditorFormController。
  2. I created data binding for NicknameTextBox's Text property - I choose SelectedPerson.Nickname from PersonEditorForm in the Data Context tab. 我为NicknameTextBox的Text属性创建了数据绑定-我从“数据上下文”选项卡的PersonEditorForm中选择SelectedPerson.Nickname。
  3. I created the following window loaded event to populate the SelectedPerson property: 我创建了以下窗口加载事件以填充SelectedPerson属性:

     private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e) { PersonEditorFormController controller = this.LayoutRoot.GetValue(Grid.DataContextProperty) as PersonEditorFormController; controller.GetPerson("Some_name"); } 

Please help. 请帮忙。 Thanks. 谢谢。

您需要在两个类上都实现INotifyPropertyChanged ,并在任何属性更改时引发PropertyChanged事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM