2017-06-16 100 views
1

我有一个观点ConfigRole其中包含的DataGrid有两列:查看和的IsEnabled(复选框)和搜索区域。覆盖记录搜索-MVVM WPF

和按钮来保存它正常工作,我做我想要的IsEnabled各方意见,我保存它: 为为例:

enter image description here

当我使用搜索框,我有正确的导致所有我在上面搜索看法,供为例我写的“客户”搜索框,我都用钥匙“客户”的看法:

enter image description here

我的问题当我做保存按钮搜索后,所有的复选框(在的IsEnabled第一视图将假!只是我在搜索视图中启用它的视图是保存!

enter image description here

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;
公共静态事件refreshList _refreshList =代表{};

public ConfigRoleModel(int RoleId,ObservableCollection<UserRoleClass> roleList) 
    { 
     test = RoleId; 
     _viewList = new ObservableCollection<ViewRoleMapClass>(WCFclient.getViewRoleMapsByRole(RoleId));  
     saveRole = new RelayCommand<Window>(configRole); 
    ConfigRoleModel._refreshList += this.refreshRoleList;  
    } 
     private void refreshRoleList() 
     {    
      _viewList = new ObservableCollection<ViewRoleMapClass>(WCFclient.getViewRoleMapsByRole(test)); 
      OnPropertyChanged("ViewList"); 
     } 

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

我的类:

public class ViewRoleMapClass : ViewModelBase 
    { 
    private int _id; 
    private bool _isEnabled; 
    private int _userRoleId; 
    private int _viewListSetId; 
    private string _viewCode; 

    public int id 
    { 
     get { return _id; } 
     set 
     { 
      _id = value; 
      ValidateAsync(); 
     } 
    } 

    public bool IsEnabled 
    { 
     get { return _isEnabled; } 
     set { _isEnabled = value; } 
    } ... 

} 
    ` 

,位于该方法的搜索,并在功能configrole中的IsEnabled(为按钮保存):if (view.IsEnabled) listViewRoleMap.Add(view); 如果这是真的,保存在列表listViewRoleMap

Web服务updateViewRoleMap:

  public int updateViewRoleMap(List<ViewRoleMapClass> listViewRoleMap, int roleId) 
     { 
      try 
     { 
      UserRole userRole = modelMiniTms.UserRoles.FirstOrDefault(a => a.Id == roleId); 
      if (userRole == null) 
       //user role is null 
       return 2; 
      List<ViewRoleMap> myListViewRoleMap = modelMiniTms.ViewRoleMaps.Where(a => a.UserRoleId == roleId).ToList(); 
      foreach (var viewRoleMap in myListViewRoleMap) 
      { 
       int index = listViewRoleMap.FindIndex(a => a.id == viewRoleMap.Id); 
       viewRoleMap.IsEnabled = index >= 0; 
       modelMiniTms.ViewRoleMaps.AddOrUpdate(viewRoleMap); 
      } 
      modelMiniTms.SaveChanges(); 

     } 
     catch (Exception ex) 
     { 
      string input = String.Empty; 
      log.WriteLogFile(userName, MethodBase.GetCurrentMethod().Name, input, ex.Message); 
      return 1; 
     } 
      log.logDataBase(userName, LogFile.OperationType.Update.ToString(), "ViewRoleMapClass", roleId.ToString()); 
     return 0; 
     } 

我该如何解决它?

感谢,

+0

检查,如果你以前保存的视图覆盖“的IsEnabled”。 –

+0

那么你在代码中设置ViewRoleMapClass的IsEnabled属性? – mm8

+0

@ mm8我已经编辑我的帖子与类和IsEnabled的小描述 – devtunis

回答

2

我猜你的问题来自于searchByCriteria方法。

我看你重新初始化_viewList集合在上述方法的第二行。这有可能是通过这样做,你正在失去你从视图中保存的东西。我知道你需要的数据,但我认为你需要的ObservableCollection<ViewRoleMapClass>属性只是为了绑定,一些沿此线:

private ObservableCollection<ViewRoleMapClass> _fullData; // replaces _viewList 
public ObservableCollection<ViewRoleMapClass> ViewList { get; private set; } 

private void searchByCriteria(string _viewName) 
{ 
    if (!string.IsNullOrEmpty(_viewName)) 
    { 
     resultSearch = _fullData.Where(c => c.ViewCode.ToLower().Contains(_viewName.ToLower())).ToList(); 
     ViewList = new ObservableCollection<ViewRoleMapClass>(resultSearch); 
    } 
    else 
     ViewList = _fullData;          
} 

在这种方式仅对象被用来填充网格被修改,而不是数据的实际来源。

而且你的构造变为:

public ConfigRoleModel(int RoleId) //You don't need that collection in the parameter list since it doesn't look like you are using it 
{ 
    test = RoleId; 
    _fullData = new ObservableCollection<ViewRoleMapClass>(WCFclient.getViewRoleMapsByRole(RoleId));  
    saveRole = new RelayCommand<Window>(configRole); 
    ViewList = _fullData; 
} 

我希望我自己清楚。

有一个愉快的一天

+0

感谢您的帮助, 但它仍然还是..when我保存的意见,同样的问题在谈到搜索,所有becmos FALSE – devtunis

+0

老启用意见@devtunis检查在'configRole'方法末尾调用的'_refreshList()'方法。无论何时更新结果,无论何时单击保存,似乎都会调用该方法。 –

+0

我已经验证了它,并且在_refreshList() – devtunis