1

マスター/詳細– datagrid / dataformがあり、アイテムを選択すると、更新のためにデータフォームに表示されますが、データバインディングまたは部門を含むcomboxへの入力に問題があり、SelectedEmployee.departmentidをselectedvalueとして設定します。

ここに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を使用する必要があると思いますが、方法がわかりません。それで、別の質問がそれを修正する方法ですか?

employeeView.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

誰かが私を助けることができますか?

comboxは空です。しかし今、私はそれを次のような部門で埋めることができます:

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; }
        } 

departmentTechManagerDomainService.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

私はこの質問の解決策を持っています。ここにあります。

于 2011-02-26T12:55:43.170 に答える
1

2番目の質問-comboboxデータフォームの内側と外側で受信が異なるため、異なるソースでプロパティDataContextを見つけようとしているようです。DepartmentsほとんどのViewModelを表示していないため、修正方法が明確ではありません。

ウィンドウに注意してVS outputください。通常、バインディングエラーに関する非常に詳細な情報が表示されます。この場合、バインディングエラーがあると想定しています。

次の方法で部門関連のバインディングを変更してみてください。

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

のxml-namespaceはどこlocalViewsにあるべきですかdepartmentTechManager.ViewsSelectedItemバインディングについても同じトリックを試してください。

于 2011-02-19T14:34:17.573 に答える
1

テンプレートの編集では、ソースをViewModel名で指定する必要があります

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