0
对WPF比较新。如果字段值从其他用户控件更改时如何将字段绑定到文本框
希望正确地做某事ViewModel设计。
我有2个用户控件。
第一个有一个下拉框,其中包含作业参考列表。
当用户选择一个我希望显示链接的字段。
所以,在我的第二个用户控件我有这个标记:
(只是在问题控制)
<TextBox Name="txtFirstName" Text="{Binding Test}" />
在后台代码:
public ucJobServiceRequirements()
{
InitializeComponent();
this.DataContext = new ViewModel.JobRecord();
}
中的代码当用户选择工作参考时,我的第一个用户控件是:
public ucCustomerJobDetails()
{
InitializeComponent();
this.DataContext = new ViewModel.JobRecord();
}
private void cboCustomerJobs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
JobHistory job = cboCustomerJobs.SelectedItem as JobHistory;
var vm = this.DataContext as ViewModel.JobRecord;
vm.JobView = InformedWorkerBusinessService.Job.Get(job.JobRef);
}
我的视图模型:
public class JobRecord : ViewModelBase
{
public JobRecord()
{
}
Job _Job;
public Job JobView
{
get { return _Job; }
set { _Job = value; RaisePropertyChanged("JobView"); Test = JobView.FirstName; }
}
string _Test;
public string Test
{
get { return _Test; }
set { _Test = value; RaisePropertyChanged("Test"); }
}
}
我ViewModelBase:
public class ViewModelBase : INotifyPropertyChanged
{
internal void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
}
public event PropertyChangedEventHandler PropertyChanged;
}
和我的模型:
public class Job
{
public string FirstName{ get; set; }
}
现在,当用户选择职位编号:
vm.JobView = InformedWorkerBusinessService.Job.Get(job.JobRef);
THI s的调用(我通过调试器检查):
set { _Test = value; RaisePropertyChanged("Test"); }
所以,我的招投标似乎是错的UI(我想!),但不能看到什么是错的。
就像我说的,我是新来的这一切。
感谢