开发者

Bind CLR object to WPF control using Microsoft Expression Blend

开发者 https://www.devze.com 2023-01-06 22:57 出处:网络
I have the following class Person: public class Person { public string Name { get { return name; } set { name = value; }

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 p开发者_Go百科oints 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消