locked
Aafter Search :overwrite record MVVM-WPF RRS feed

  • Question

  • the first View : Datagrid with all the views and checkbox IsEnabled or not: 

    I make Isenabled for whic Views I want and then Save,It works correct,

    • Then I do the search , It works fine:

    and I make the CheckBox which I want..

    ** The problem: after I make Save , I have just the saved which are in the Search result and all the others will Disabled!! this result after save after search: 

    I have all the old Views overwrite (Disabled!!) and I have Enabled just AllTrailer and AddTrailer (which I make it Enabled from the search result)

    Xaml:

    <TextBox Text="{Binding ViewName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True}" >
    
    <Button x:Name="button1"  Command="{Binding  SaveRole}" >

    my ViewModel:ConfigRoleModel:
    //My List
         public ObservableCollection<ViewRoleMapClass> ViewList
             {
                 get { return _viewList; }
                 set
                 {
                     _viewList = value;
                     OnPropertyChanged("ViewList");
                 }
             } 
    
              public ConfigRoleModel(int RoleId,ObservableCollection<UserRoleClass> roleList)
        {
            test = RoleId;
            //I have all my List of Views Correct
            _viewList = new ObservableCollection<ViewRoleMapClass>(WCFclient.getViewRoleMapsByRole(RoleId));         
    
             _addOrUpdate = new UserRoleClass();
             saveRole = new RelayCommand<Window>(configRole);                      
        }
    
    
          private void configRole(Window window)
        {     
             List<ViewRoleMapClass> listViewRoleMap = new List<ViewRoleMapClass>();
            foreach (ViewRoleMapClass view in ViewList)
            {
                if (view.IsEnabled) listViewRoleMap.Add(view);
            }                   
    
          try
            {    //Web service: save the new modification
                int resultUpdate = WCFclient.updateViewRoleMap(listViewRoleMap, test);          
    
                 if (resultUpdate == 0)
                {                  
                    string sInformation = "Save operation is performed successfully";
                    MessageBoxButton btnMessageBox = MessageBoxButton.OK;
                    MessageBoxImage icnMessageBox = MessageBoxImage.Information;
                    MessageBoxResult rsltMessageBox = MessageBox.Show(sInformation, sCaption, btnMessageBox, icnMessageBox);                   
                }               
                _refreshList();
            }

    How can I fix it? I hope that my explanation is clear

    Thanks,

    Wednesday, June 14, 2017 9:47 AM

All replies

  • Hi,

    >>** The problem: after I make Save , I have just the saved which are in the Search result and all the others will Disabled!! this result after save after search: 

    For some logical problems, we really have no way to help you analyze, because we know nothing about your program, the code you provided did not help me better understand your program, the best way is  debugging step by step to achieve the desired results.

    BTW, according to your description, I'm not really following what problem you're trying to solve, your grammar and punctuation makes what you're saying hard to follow. (No offense.)

    You should bind the Checkbox with a dependency property of Model.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 15, 2017 6:41 AM
  • Very clear,

    Thanks a lot

    Friday, June 16, 2017 9:25 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:

            <TextBox x:Name="textBox" Text="{Binding ViewName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
             NotifyOnValidationError=True ,TargetNullValue=''}" />
    		 
    		 <DataGrid x:Name="dataGrid"  SelectedItem="{Binding SelectedView}" ItemsSource="{Binding ViewList}"   
                       CanUserAddRows="False" AlternationCount="2" AlternatingRowBackground="Blue" AutoGenerateColumns="False" >
    				   
    				    <DataGrid.Columns>
                    <DataGridTextColumn Header="View" Binding="{Binding ViewCode}"  IsReadOnly="True" />
    
                    <DataGridTemplateColumn Header="Is Enabled" Width="Auto">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding IsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                   </DataGridTemplateColumn>       
                </DataGrid.Columns>
            
    		   </DataGrid>
    		<Button Command="{Binding  SaveRole}"  Visibility="{Binding Path=ShowSaveButton, Converter={StaticResource BoolToVis}}" CommandParameter="{Binding ElementName=ConfigureRole}"/>
    		
        </Grid>
    		

    ConfigRoleViewModel:

             private ObservableCollection<ViewRoleMapClass> _viewList;
             private MiniTasServicesClient WCFclient = new MiniTasServicesClient();        
             public int test;
    		 
    		  public ConfigRoleModel(int RoleId,ObservableCollection<UserRoleClass> roleList)
            {
                test = RoleId;
                _viewList = new ObservableCollection<ViewRoleMapClass>(WCFclient.getViewRoleMapsByRole(RoleId));       
                 saveRole = new RelayCommand<Window>(configRole);       
            }   
    		 
             private RelayCommand<Window> saveRole;
             public RelayCommand<Window> SaveRole
            {
                get { return saveRole; }
            }
    		          
            
    		//all list of Views 
    		public ObservableCollection<ViewRoleMapClass> ViewList
                 {
                     get { return _viewList; }
                     set
                     {
                         _viewList = value;
                         OnPropertyChanged("ViewList");
                     }
                 }  
    			 
    			 //the Function of the Button Save
    			  private void configRole(Window window)
            {     
                 List<ViewRoleMapClass> listViewRoleMap = new List<ViewRoleMapClass>();
                foreach (ViewRoleMapClass view in ViewList)
                {
                    if (view.IsEnabled) listViewRoleMap.Add(view);
                }    
    			 int resultUpdate = WCFclient.updateViewRoleMap(listViewRoleMap, test);
    			 if (resultUpdate == 0)
                    {
                        string sCaption = "Save notification";
                        string sInformation = "Save operation is performed successfully";
                        MessageBoxButton btnMessageBox = MessageBoxButton.OK;
                        MessageBoxImage icnMessageBox = MessageBoxImage.Information;
    
                        MessageBoxResult rsltMessageBox = MessageBox.Show(sInformation, sCaption, btnMessageBox, icnMessageBox);                   
                    }               
                    _refreshList();
    		}
    		
    		//Search		
    		     private string _viewName;
                 public string ViewName
                 {
                     get { return _viewName; }
                     set
                     {
                         _viewName = value;
                         OnPropertyChanged("ViewName");
                        _viewList = searchByCriteria(ViewName);
                         OnPropertyChanged("ViewList");
                     }
                 }			 
    			  private ObservableCollection<ViewRoleMapClass> searchByCriteria(string _viewName)
                 {
                     List<ViewRoleMapClass> resultSearch=new List<ViewRoleMapClass>();                 
                     _viewList = new ObservableCollection<ViewRoleMapClass>(WCFclient.getViewRoleMapsByRole(test));                 
                     
                     if (_viewName != null)
                     {
                         resultSearch = _viewList.Where(c => c.ViewCode.ToLower().Contains(_viewName.ToLower())).ToList();                       
                     }                   
                     return new ObservableCollection<ViewRoleMapClass>(resultSearch);                            
                 }

    How can I fix it?

    Thanks,

    • Merged by Bob Ding Monday, June 19, 2017 9:50 AM same
    Friday, June 16, 2017 9:30 AM
  • Hi,

    Your two questions seem to be similar, so I merge your two questions.

    For your question,  please provide a simple project via OneDrive, I will test on my side and try to give you a solution to resolve it. 

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 19, 2017 10:07 AM