1

我有主/详细信息 - 数据网格/数据表单,在选择项目后它显示在数据表单中以进行更新,但是我在数据绑定或用部门填充组合框并将 SelectedEmployee.departmentid 设置为选定值时遇到问题。

现在这里有2个问题:

1. 在 EmployeeViewModel 中,这段代码不起作用,为什么?

  private ObservableCollection<department> _departments;
        public ObservableCollection<department> Departments
        {
            get { return _departments; }
            set
            {
                _departments = value;
                RaisePropertyChanged("Departments");
            }
        }

但是这段代码工作正常

   private ObservableCollection<department> _departments;
        public ObservableCollection<department> Departments
        {
            get {

                if (_departments == null)
                {
                    _departments = new ObservableCollection<department> {
                        new department()
                        { id = 1, departmentname = "Technical " + 1, },
                        new department()
                        { id = 2,  departmentname = "Technical " + 2, },
                        new department()
                        { id = 3,  departmentname = "Technical " + 3, }                    
                    };
                }
                  return _departments; 
            }
            set
            {
                _departments = value;
                RaisePropertyChanged("Departments");
            }
        }

2. DataForm 内外Combobox 的行为不同。外面可以,里面不行。我认为这里需要在ItemsSource中使用Source,但我不知道如何。那么还有一个问题是如何解决呢?

员工视图.xaml

<navigation:Page    xmlns:local="clr-namespace:departmentTechManager"
            x:Class="departmentTechManager.Views.employeeView" 
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                   mc:Ignorable="d"
                   xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
                   d:DesignWidth="820" d:DesignHeight="780"
                   Title="employees"
                   Style="{StaticResource PageStyle}"

            xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
            xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
            xmlns:mvvmlightcmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
            xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
            xmlns:dataformtoolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit" 

            DataContext="{Binding employeeStatic, Source={StaticResource Locator}}">

<data:DataGrid Grid.Row="0" x:Name="dgEmployees" CanUserSortColumns="true"
                                             IsReadOnly="true" AutoGenerateColumns="true"
                                             ItemsSource="{Binding Employees}"
                                             SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}"  Margin="0,36,-123,0"></data:DataGrid>
<dataformtoolkit:DataForm x:Name="dfDetails"
      CurrentItem="{Binding SelectedEmployee}" AutoGenerateFields="False"
      CommitButtonContent="Save" CommandButtonsVisibility="Edit, Commit, Cancel">
      <dataformtoolkit:DataForm.EditTemplate>
                      <DataTemplate>
                        <StackPanel>
                           <dataformtoolkit:DataField Label="name">
    <TextBox Text="{Binding name, Mode=TwoWay}" /></dataformtoolkit:DataField>

      <dataformtoolkit:DataField Label="departments">


            <ComboBox ItemsSource="{Binding Departments}"  
        DisplayMemberPath="departmentname"
            SelectedValuePath="id"  
            SelectedValue="{Binding Path=SelectedEmployee.departmentid, Mode=TwoWay}" />


     </dataformtoolkit:DataField>
      </StackPanel>
     </DataTemplate>
            </dataformtoolkit:DataForm.EditTemplate>
            <i:Interaction.Triggers><i:EventTrigger EventName="EditEnded">
                <mvvmlightcmd:EventToCommand Command="{Binding SaveEmployeesCommand}"/>
               </i:EventTrigger></i:Interaction.Triggers>
            </dataformtoolkit:DataForm>

在 ViewModelLocator.cs 中:

 public ViewModelLocator()
        {

            _sp = ServiceProviderBase.Instance;

            Createdepartment();

            Createemployee();

        }

#region EmployeeViewModel
private static EmployeeViewModel _employee;

public static EmployeeViewModel employeeStatic
{
    get
    {
        if (_employee == null)
        {
            Createemployee();
        }

        return _employee;
    }
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
    "CA1822:MarkMembersAsStatic",
    Justification = "This non-static member is needed for data binding purposes.")]
public EmployeeViewModel employee
{
    get
    {
        return employeeStatic;
    }
}

public static void Clearemployee()
{
    //do it later
    //_employee.Cleanup();
    _employee = null;
}

public static void Createemployee()
{
    if (_employee == null)
    {
        _employee = new EmployeeViewModel(_sp.PageConductor, _sp.EmployeeDataService);
    }
}
#endregion

#region DepartmentViewModel
        private static DepartmentViewModel _department;

        public static DepartmentViewModel departmentStatic
        {
            get
            {
                if (_department == null)
                {
                    Createdepartment();
                }

                return _department;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public DepartmentViewModel department
        {
            get
            {
                return departmentStatic;
            }
        }

        public static void Cleardepartment()
        {
            //do it later
            //_department.Cleanup();
            _department = null;
        }

        public static void Createdepartment()
        {
            if (_department == null)
            {
                _department = new DepartmentViewModel(_sp.PageConductor, _sp.DepartmentDataService);
            }
        }

        #endregion

有人可以帮助我吗?

组合框只是空的。但现在我可以用这样的部门填充它:

departmentTechManagerDomainService.metadata.cs

 [MetadataTypeAttribute(typeof(employee.employeeMetadata))]
        public partial class employee
        {
     [Include]
     public department department { get; set; }
     public Nullable<int> departmentid { get; set; }
     public string name { get; set; }
        } 

部门TechManagerDomainService.cs

public IQueryable<employee> GetEmployees()
        {return this.ObjectContext.employees.Include("department").OrderBy(e=>e.name);}

这是 ViewModel 代码:

        private ObservableCollection<department> _departments;
        public ObservableCollection<department> Departments
        {
            get { return _departments; }
            set
            {
                _departments = value;
                RaisePropertyChanged("Departments");
            }
        }

        private department _selectedDepartment;
        public department SelectedDepartment
        {
            get { return _selectedDepartment; }
            set
            {
                _selectedDepartment = value;
                RaisePropertyChanged("SelectedDepartment");
            }
        }

private void InitializeModels()
        {
            Employees = new ObservableCollection<employee>();
            SelectedEmployee = new employee();
            NewEmployee = new employee();

            //new
            Departments = new ObservableCollection<department>();
            SelectedDepartment = new department();

        }

private void GetEmployeesCallback(IEnumerable<employee> employees)
        {
            if (employees != null)
            {
                foreach (var employee in employees)
                {
                    Employees.Add(employee);
                    //new
                    if (!Departments.Contains(employee.department))
                        Departments.Add(employee.department);

                }
                if (Employees.Count > 0)
                {
                    SelectedEmployee = Employees[0];
                }

            }
        }

我将部门区分,但这里只有那些已经被选中的部门,但这里不是那些尚未被选中的部门,而且组合框仍然没有填充 DataForm 中的部门。?!

4

3 回答 3

1

第二个问题 - 看起来combobox数据表单的内部和外部接收不同DataContext,因此试图定位Departments不同来源的属性。目前尚不清楚如何修复它,因为您还没有展示大部分 ViewModel。

注意VS output窗口,它通常提供有关绑定错误的非常详细的信息,我假设您的情况存在绑定错误。

尝试通过以下方式修改与部门相关的绑定:

<ComboBox ItemsSource="{Binding DataContext.Departments, RelativeSoruce={RelativeSource AncestorType={x:Type localViews:employeeView}}}" />

哪里localViews应该是 .xml 的 xml 命名空间departmentTechManager.Views。尝试相同的SelectedItem绑定技巧。

于 2011-02-19T14:34:17.573 回答
1

我有这个问题的解决方案。在这里

于 2011-02-26T12:55:43.170 回答
1

在 Edit Template 中,Source 必须与 ViewModel Name 一起提及

   <ComboBox Grid.Row="2" Grid.Column="1"
    ItemsSource="{Binding Path=Accounts, Source={StaticResource MyAccountViewModel}, Mode=TwoWay}" />
于 2012-01-03T21:03:06.533 回答