我的ViewModel更改未反映在我的UI中

时间:2016-05-14 08:36:40

标签: c# wpf mvvm

我的视图中有两个绑定的文本框。

<TextBox Text="{Binding BookingWizard.CustomerView.Email,Mode=TwoWay}" />
<TextBox Text="{Binding BookingWizard.CustomerView.ContactNo,Mode=TwoWay}" />

当另一个文本框失去焦点时,我可以填充这些字段。该位背后的代码是:

private void txtFirstName_LostFocus(object sender, RoutedEventArgs e)
{
    LookUpEmailAndContactNo();
}

private void LookUpEmailAndContactNo()
{
    var vm = this.DataContext as ApplicationViewModel;
    var customer = vm.BookingWizard.LookUpEmailAndContactNo();

    //etc        
    vm.BookingWizard.CustomerView.Email = customer.Email;

}

public Customer LookUpEmailAndContactNo()
{
    var res= InformedWorkerBusinessService.Customer.GetEmailAndContactNo(CustomerView.FName, CustomerView.SName);
    if (res!=null)
    {
        CustomerView.Email = res.Email;
        CustomerView.ContactNo = res.ContactNo;
    }
    return CustomerView;
}

当我在 LookUpEmailAndContactNo 事件中设置断点时,这是我数据上下文的屏幕截图:

enter image description here

正如您所看到的,数据上下文确实具有这些值,但我看不出我的UI绑定有什么问题?

其他

我在App入口点设置了我的视图模型:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        ApplicationView app = new ApplicationView();
        ApplicationViewModel context = new ApplicationViewModel();
        context.ActiveRecord = new ActiveRecord();
        context.CustomerSearch = new CustomerSearch();
        context.BookingWizard = new BookingWizard();
        context.BookingWizard.CustomerView = new InformedWorkerModel.Customer();
        context.BookingWizard.JobView = new InformedWorkerModel.Job();
        app.DataContext = context;
        app.Show();
    }
}

这是在我的BookingWizard类中:

public class BookingWizard : ViewModelBase, IDataErrorInfo
{
    Customer _Customer; 

    public bool IsExistingCustomer { get; set; }

    public IEnumerable<string> FNames
    {
        get
        {
            if (CustomerView.SName == null)
            {
                CustomerView.SName = string.Empty;
            }
            return InformedWorkerBusinessService.Customer.GetFirstNames(CustomerView.SName);
        }
    }

    public Customer LookUpEmailAndContactNo()
    {
        var res= InformedWorkerBusinessService.Customer.GetEmailAndContactNo(CustomerView.FName, CustomerView.SName);
        if (res!=null)
        {
            CustomerView.Email = res.Email;
            CustomerView.ContactNo = res.ContactNo;
        }
        return CustomerView;
    }

    public Customer CustomerView
    {
        get { return _Customer; }
        set
        {
            _Customer = value; RaisePropertyChanged("CustomerView");
        }
    }
}

和我的客户类:

[Table("Customer")]
public  class Customer
{
    [AutoIncrement]
    [PrimaryKey]
    public int CustomerId { get; set; }   

    public string SName { get; set; }
    public string FName { get; set; }     
    public string ContactNo { get; set; }
    public string Email { get ; set; }
}

0 个答案:

没有答案