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

发布于 2024-09-09 00:43:17 字数 2173 浏览 0 评论 0原文

我有以下类 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"]);
     }
}

和不同程序集中的另一个类 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 是一个提供从数据库获取数据和构造 person 对象的方法的类。

现在,PersonEditorForm.xaml 有一个名为 NicknameTextBox 的文本框。我想使用 Microsoft Expression Blend 将人员的昵称绑定到其 Text 属性。我该怎么做?

这是我到目前为止所尝试的(没有成功):

  1. 我为 LayoutRoot 创建了新的对象数据源,它指向我的引擎类,即 PersonEditorFormController。
  2. 我为 NicknameTextBox 的 Text 属性创建了数据绑定 - 我从“数据上下文”选项卡中的 PersonEditorForm 选择 SelectedPerson.Nickname。
  3. 我创建了以下窗口加载事件来填充 SelectedPerson 属性:

    private void Window_Loaded(对象发送者,System.Windows.RoulatedEventArgs e)
    {
         PersonEditorFormController 控制器 = 
             this.LayoutRoot.GetValue(Grid.DataContextProperty) 作为 PersonEditorFormController;
         控制器.GetPerson("Some_name");
    }
    

请帮忙。谢谢。

I have the following class 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:

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.

Now, PersonEditorForm.xaml has one text box called NicknameTextBox. I want to bind the nickname of the person to its Text property using Microsoft Expression Blend. 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, i.e. PersonEditorFormController.
  2. I created data binding for NicknameTextBox's Text property - I choose SelectedPerson.Nickname from PersonEditorForm in the Data Context tab.
  3. I created the following window loaded event to populate the SelectedPerson property:

    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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香橙ぽ 2024-09-16 00:43:17

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

You need to implement INotifyPropertyChanged on both of your classes and raise the PropertyChanged event whenever any property changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文