使用 Microsoft Expression Blend 将 CLR 对象绑定到 WPF 控件
我有以下类 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 属性。我该怎么做?
这是我到目前为止所尝试的(没有成功):
- 我为 LayoutRoot 创建了新的对象数据源,它指向我的引擎类,即 PersonEditorFormController。
- 我为 NicknameTextBox 的 Text 属性创建了数据绑定 - 我从“数据上下文”选项卡中的 PersonEditorForm 选择 SelectedPerson.Nickname。
我创建了以下窗口加载事件来填充 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):
- I created new Object Data Source to LayoutRoot that points to my engine class, i.e. PersonEditorFormController.
- I created data binding for NicknameTextBox's Text property - I choose SelectedPerson.Nickname from PersonEditorForm in the Data Context tab.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在两个类上实现
INotifyPropertyChanged
,并在任何属性发生更改时引发PropertyChanged
事件。You need to implement
INotifyPropertyChanged
on both of your classes and raise thePropertyChanged
event whenever any property changes.