Silverlight MVVM-如何使用ViewModel声明性地绑定DataGrid CellEditingTemplate中的组合框?
我正在尝试使用ViewModel声明性地绑定DataGrid CellEditingTemplate中的组合框。组合框未被绑定。我做错了什么 XAML:Silverlight MVVM-如何使用ViewModel声明性地绑定DataGrid CellEditingTemplate中的组合框?,silverlight,mvvm,silverlight-4.0,Silverlight,Mvvm,Silverlight 4.0,我正在尝试使用ViewModel声明性地绑定DataGrid CellEditingTemplate中的组合框。组合框未被绑定。我做错了什么 XAML: 视图模型: using System.Collections.ObjectModel; using SilverlightApplication1.EmployeeService; using SilverlightApplication1.ViewModels; namespace SilverlightApplication1 {
视图模型:
using System.Collections.ObjectModel;
using SilverlightApplication1.EmployeeService;
using SilverlightApplication1.ViewModels;
namespace SilverlightApplication1
{
public class EmployeeDetailsViewModel : ViewModelBase
{
readonly IEmployeeServiceAgent _serviceAgent;
ObservableCollection<EmployeeStatus> _employeeStatuses { get; set; }
ObservableCollection<Employee> _employees { get; set; }
public EmployeeDetailsViewModel() : this(new EmployeeServiceAgent()) { }
public EmployeeDetailsViewModel(IEmployeeServiceAgent serviceAgent)
{
if (!IsDesignTime)
{
_serviceAgent = serviceAgent;
GetAllEmployees();
GetEmployeeStatuses();
}
}
public ObservableCollection<Employee> Employees
{
get { return _employees; }
set
{
if(_employees!=value)
{
_employees = value;
OnNotifyPropertyChanged("Employees");
}
}
}
public ObservableCollection<EmployeeStatus> EmployeeStatuses
{
get { return _employeeStatuses; }
set
{
if (_employeeStatuses != value)
{
_employeeStatuses = value;
OnNotifyPropertyChanged("EmployeeStatuses");
}
}
}
private void GetAllEmployees()
{
_serviceAgent.GetAll((s, e) => Employees = e.Result);
}
private void GetEmployeeStatuses()
{
_serviceAgent.GetEmployeeStatuses((s, e) => EmployeeStatuses = e.Result);
}
}
}
使用System.Collections.ObjectModel;
使用SilverlightApplication1.EmployeeService;
使用SilverlightApplication1.ViewModels;
命名空间SilverlightApplication1
{
公共类EmployeeDetailsViewModel:ViewModelBase
{
只读IEmployeeServiceAgent\u serviceAgent;
ObservableCollection_employeeStatuses{get;set;}
ObservableCollection{get;set;}
public EmployeeDetailsViewModel():此(新EmployeeServiceAgent()){}
公共雇员详细信息IEWModel(IEEmployeeServiceAgent服务代理)
{
如果(!IsDesignTime)
{
_serviceAgent=serviceAgent;
GetAllEmployees();
GetEmployeeStatuses();
}
}
公开收集雇员
{
获取{return\u employees;}
设置
{
如果(_employees!=值)
{
_员工=价值;
OnNotifyPropertyChanged(“员工”);
}
}
}
公共可观察收集员工状态
{
获取{return\u employeestatus;}
设置
{
如果(_employeestatus!=值)
{
_EmployeeStatus=值;
OnNotifyPropertyChanged(“员工状态”);
}
}
}
私人职位空缺GetAllEmployees()
{
_GetAll((s,e)=>Employees=e.Result);
}
私有void GetEmployeeStatuses()
{
_serviceAgent.GetEmployeeStatuses((s,e)=>EmployeeStatuses=e.Result);
}
}
}
更新:
这似乎是错误的,但我通过在ItemSource绑定中重新引用ViewModel找到了如何使绑定工作的方法:
<ComboBox ItemsSource="{Binding Source={StaticResource ViewModel},Path=EmployeeStatuses}"
DisplayMemberPath="Description"
SelectedItem="{Binding EmployeeStatus, Mode=TwoWay}" />
但是,我现在遇到一个问题,SelectedItem未绑定!我做错了什么?这个问题是人们经常遇到的问题。在列的数据模板中时,不再绑定视图模型。此时,您的数据上下文是EmployeeStatus对象(它没有要绑定的EmployeeStatuses属性) 因此,要使combobox绑定正常工作,可以使用ElementName=LayoutRoot将树绑定回根ViewModel 更新:以下是绑定的完整语法: {Binding DataContext.EmployeeStatuses,ElementName=LayoutRoot}
Update2:我实际上也遇到过这个问题,有一种方法可以让元素名绑定在数据网格中工作 如果Bryant的解决方案不起作用(在SL4中),请使用静态资源。请参阅此链接:
或者,通过在xaml中创建静态资源:您的意思是将ElementName=LayoutRoot添加到组合框的ItemsSource中吗?我这样做了,但是组合框仍然没有绑定。布莱恩特,我试过了,但似乎也没用
<ComboBox ItemsSource="{Binding Source={StaticResource ViewModel},Path=EmployeeStatuses}"
DisplayMemberPath="Description"
SelectedItem="{Binding EmployeeStatus, Mode=TwoWay}" />