none
how to change loadoperation.entities?

    Question

  • Tongue out

    when i use domainservice's load method to load a query, as it retured the loadoperation.entities collection is readonly

    and I can't change it.  what i want to do is fetching entitiesfrom domainservice and bind it to a gridview. i want to remove an entity from gridview and change database. here is my code snipper.

    I met two problems:

    1: visual studio prompt me that the loadoperation.entities collection is readonly and can't change

    2: commandparameter not be passed to viewmodel

    Follow is my code snipper:

    [view.xaml markup]

    <StackPanel >
                <telerik:RadButton Content="Add" Height="Auto" Name="btnAdd" VerticalAlignment="Top"Command="{Binding AddCommand}" />
                <telerik:RadButton Content="Edit" Height="Auto" Name="btnEdit"  Command="{Binding EditCommand}" CommandParameter="{Binding ElementName=departmentGridView,Path=SelectedItem}" />
                <telerik:RadButton Content="Delete" Name="btnDelete" Command="{Binding DeleteCommand}"  CommandParameter="{Binding ElementName=departmentGridView,Path=SelectedItem}" />
            </StackPanel>
            <telerik:RadGridView  
                                 HorizontalAlignment="Stretch"
                                 VerticalAlignment="Stretch" 
                                 ShowGroupPanel="False" 
                                 ShowColumnHeaders="True"
                                 RowIndicatorVisibility="Collapsed"
                                 ItemsSource="{Binding DepartmentTypeList}"
                                 Name="departmentTypeGridView" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" AutoGenerateColumns="False" Grid.RowSpan="1" Grid.Row="1">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn Header="Name" Width="*" DataMemberBinding="{Binding Name}" IsSortable="False" />
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
    [viewmodel code]
    private readonly DepartmentTypeContext _DomainContext;
    
            public DepartmentTypeViewModel()
            {
                this._DomainContext = new DepartmentTypeContext();
                this._DeleteCommand = new DelegateCommand(this.Delete,()=>_DepartmentTypeList!=null && _DepartmentTypeList.CurrentItem!=null?true:false);
    
                LoadDepartmentType();
            }
    
            void _DepartmentTypeList_CurrentChanged(object sender, System.EventArgs e)
            {
                (_DeleteCommand as DelegateCommand).RaiseCanExecuteChanged();
            }
    
            private void LoadDepartmentType()
            {
                _DomainContext.Load<DepartmentType>(_DomainContext.GetDepartmentTypeQuery(),this.LoadCallBack,null);
            }
    
            private void LoadCallBack(LoadOperation<DepartmentType> lo)
            {
                if (lo.HasError)
                {
                    MessageBox.Show(lo.Error.Message);
                    return;
                }
                CollectionViewSource dataSource = new CollectionViewSource();
                dataSource.Source = lo.AllEntities;
                DepartmentTypeList = dataSource.View;
                DepartmentTypeList.MoveCurrentToFirst();
                DepartmentTypeList.CurrentChanged += new System.EventHandler(_DepartmentTypeList_CurrentChanged);
            }
    
            private ICollectionView _DepartmentTypeList;
    
            public ICollectionView DepartmentTypeList
            {
                get
                {
                    return _DepartmentTypeList;
                }
                private set
                {
                    if (_DepartmentTypeList != value)
                    {
                        _DepartmentTypeList = value;
                        RaisePropertyChanged(() => this.DepartmentTypeList);
                    }
                }
            }
    
            private ICommand _AddCommand;
            public ICommand AddCommand
            {
                get
                {
                    return _AddCommand;
                }
                set
                {
                    if (_AddCommand != value)
                    {
                        this._AddCommand = value;
                        RaisePropertyChanged(() => this._AddCommand);
                    }
                }
            }
    
            private ICommand _EditCommand;
            public ICommand EditCommand
            {
                get
                {
                    return _EditCommand;
                }
                set
                {
                    if (_EditCommand != value)
                    {
                        this._EditCommand = value;
                        RaisePropertyChanged(() => this._EditCommand);
                    }
                }
            }
    
            private ICommand _DeleteCommand;
            public ICommand DeleteCommand
            {
                get
                {
                    return _DeleteCommand;
                }
                set
                {
                    if (_DeleteCommand != value)
                    {
                        _DeleteCommand = value;
                        RaisePropertyChanged(() => this._DeleteCommand);
                    }
                }
            }
    
            private void Delete()
            {
                if (DepartmentTypeList.CurrentItem != null)
                {   
                    IList list = DepartmentTypeList.SourceCollection as IList;
                    list.Remove(DepartmentTypeList.CurrentItem);
                }
            }
    Friday, April 27, 2012 11:54 AM

Answers

All replies