2017-06-16 341 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