paul paul

paul paul

  • NA
  • 296
  • 6k

Overwrite record after Search -MVVM WPF

Jun 16 2017 8:24 AM

I have a View ConfigRole which contains DataGrid with two Columns: View and IsEnabled(CheckBox), and a Search area.

and the Button Save it works correctly, I make all views which I want IsEnabled and I save it : for exemple:
 
And when I use the search box, I have the correct result for all the views I search on it,for exemple I write 'Customer' in search box ,I have all the views with the key 'Customer':
 
 
My problem is when I make Save Button after Search ,all the CheckBox (IsEnabled in the first View will be FALSE !! just the Views I make it Enabled in the Search view are Save !
 
 
 
XAML ConfigRole:
 
 
  1.      <TextBox x:Name="textBox" Text="{Binding ViewName, Mode=TwoWayUpdateSourceTrigger=PropertyChanged,  
  2.       NotifyOnValidationError=True ,TargetNullValue=''}" />  
  3.   
  4. <DataGrid x:Name="dataGrid"  SelectedItem="{Binding SelectedView}" ItemsSource="{Binding ViewList}"     
  5.                 CanUserAddRows="False" AlternationCount="2" AlternatingRowBackground="Blue" AutoGenerateColumns="False" >  
  6.          
  7.         <DataGrid.Columns>  
  8.              <DataGridTextColumn Header="View" Binding="{Binding ViewCode}"  IsReadOnly="True" />  
  9.   
  10.              <DataGridTemplateColumn Header="Is Enabled" Width="Auto">  
  11.                  <DataGridTemplateColumn.CellTemplate>  
  12.                      <DataTemplate>  
  13.                          <CheckBox IsChecked="{Binding IsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>  
  14.                      </DataTemplate>  
  15.                  </DataGridTemplateColumn.CellTemplate>  
  16.             </DataGridTemplateColumn>         
  17.          </DataGrid.Columns>  
  18.        
  19.   </DataGrid>  
  20. lt;Button Command="{Binding  SaveRole}"  Visibility="{Binding Path=ShowSaveButton, Converter={StaticResource BoolToVis}}" CommandParameter="{Binding ElementName=ConfigureRole}"/>  
  21.   
  22.  </Grid>  
 ConfigRoleViewModel:
 
  1.        private ObservableCollection<ViewRoleMapClass> _viewList;  
  2.        private MiniTasServicesClient WCFclient = new MiniTasServicesClient();          
  3.        public int test;  
  4.    
  5.   public ConfigRoleModel(int RoleId,ObservableCollection<UserRoleClass> roleList)  
  6.       {  
  7.           test = RoleId;  
  8.           _viewList = new ObservableCollection<ViewRoleMapClass>(WCFclient.getViewRoleMapsByRole(RoleId));         
  9.            saveRole = new RelayCommand<Window>(configRole);         
  10.       }     
  11.    
  12.        private RelayCommand<Window> saveRole;  
  13.        public RelayCommand<Window> SaveRole  
  14.       {  
  15.           get { return saveRole; }  
  16.       }  
  17.             
  18.         
  19. //all list of Views   
  20. public ObservableCollection<ViewRoleMapClass> ViewList  
  21.            {  
  22.                get { return _viewList; }  
  23.                set  
  24.                {  
  25.                    _viewList = value;  
  26.                    OnPropertyChanged("ViewList");  
  27.                }  
  28.            }    
  29.        
  30.      //the Function of the Button Save  
  31.       private void configRole(Window window)  
  32.       {       
  33.            List<ViewRoleMapClass> listViewRoleMap = new List<ViewRoleMapClass>();  
  34.           foreach (ViewRoleMapClass view in ViewList)  
  35.           {  
  36.               if (view.IsEnabled) listViewRoleMap.Add(view);  
  37.           }      
  38.      int resultUpdate = WCFclient.updateViewRoleMap(listViewRoleMap, test);  
  39.      if (resultUpdate == 0)  
  40.               {  
  41.                   string sCaption = "Save notification";  
  42.                   string sInformation = "Save operation is performed successfully";  
  43.                   MessageBoxButton btnMessageBox = MessageBoxButton.OK;  
  44.                   MessageBoxImage icnMessageBox = MessageBoxImage.Information;  
  45.   
  46.                   MessageBoxResult rsltMessageBox = MessageBox.Show(sInformation, sCaption, btnMessageBox, icnMessageBox);                     
  47.               }                 
  48.               _refreshList();  
  49. }  
  50.   
  51. //Search          
  52.      private string _viewName;  
  53.            public string ViewName  
  54.            {  
  55.                get { return _viewName; }  
  56.                set  
  57.                {  
  58.                    _viewName = value;  
  59.                    OnPropertyChanged("ViewName");  
  60.                   _viewList = searchByCriteria(ViewName);  
  61.                    OnPropertyChanged("ViewList");  
  62.                }  
  63.            }               
  64.       private ObservableCollection<ViewRoleMapClass> searchByCriteria(string _viewName)  
  65.            {  
  66.                List<ViewRoleMapClass> resultSearch=new List<ViewRoleMapClass>();                   
  67.                _viewList = new ObservableCollection<ViewRoleMapClass>(WCFclient.getViewRoleMapsByRole(test));                   
  68.                  
  69.                if (_viewName != null)  
  70.                {  
  71.                    resultSearch = _viewList.Where(c => c.ViewCode.ToLower().Contains(_viewName.ToLower())).ToList();                         
  72.                }                     
  73.                return new ObservableCollection<ViewRoleMapClass>(resultSearch);                              
  74.            }  

How can I fix it?

Thanks,

 

Answers (1)