Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
Imgur: The most awesome images on the Internet[^]

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':
Imgur: The most awesome images on the Internet[^]

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 !Imgur: The most awesome images on the Internet[^]





How can I fix it?

Thanks,

What I have tried:

XAML ConfigRole:
XML
<pre>

        <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:

C#
<pre>
         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);                            
             }
Posted
Updated 16-Jun-17 5:00am
v3

1 solution

It looks like in your code you are replacing your ViewName with a new ObservableCollection every time you update it which means you are breaking your reference that held the previous state of the object. If you are not preserving this information and making sure you update it back in, then what you are doing is overwriting it with a new object where in the case of your IsEnabled property the default boolean value is False.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900