【问题标题】:Overwrite record after Search -MVVM WPF搜索后覆盖记录 -MVVM WPF
【发布时间】:2017-06-18 08:32:27
【问题描述】:

我有一个 View ConfigRole,其中包含带有两列的 DataGrid:View 和 IsEnabled(CheckBox),以及一个搜索区域。

并且按钮保存它工作正常,我制作了我想要 IsEnabled 的所有视图并保存它: 例如:

当我使用搜索框时,我在其上搜索的所有视图都有正确的结果,例如我在搜索框中写了“客户”,我有所有带有“客户”键的视图:

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

XAML 配置角色:`

    <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 = delegate { };

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

 }  
   `

IsEnabled 位于方法搜索和函数配置中(用于按钮保存):if (view.IsEnabled) listViewRoleMap.Add(view); 如果为TRUE,则保存在列表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;
        }

我该如何解决?

谢谢,

【问题讨论】:

  • 检查您是否正在覆盖以前保存的视图的“IsEnabled”。
  • 那么您在代码中哪里设置 ViewRoleMapClass 的 IsEnabled 属性?
  • @mm8 我已经用 IsEnabled 的类和少量描述编辑了我的帖子
  • 目前还不清楚你在哪里设置属性。
  • 我添加Web服务的IsEnabled的保存位置,现在可以了吗? Web 服务只是我调用它..

标签: c# wpf mvvm


【解决方案1】:

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

我看到您正在上述方法的第二行中重新初始化 _viewList 集合。这样做可能会丢失从视图中保存的内容。我知道您需要数据,但我认为您需要 ObservableCollection&lt;ViewRoleMapClass&gt; 的属性来进行绑定,类似于以下内容:

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

我希望我说清楚了。

祝你有美好的一天

【讨论】:

  • 感谢您的帮助,但它仍然是同样的问题..当我保存视图进入搜索时,所有旧启用的视图都变为 FALSE
  • @devtunis 检查在configRole 方法结束时调用的_refreshList() 方法。无论更新结果如何,似乎每当您单击保存时都会调用该方法。
  • 我已经验证过了,没发现_refreshList()有什么问题
  • 我在没有服务调用的情况下做了一个小例子,我在上面发布的代码按预期工作。在我查看了您后来添加的内容后,似乎问题出在 _refreshList() 方法中。
  • 我已经编辑了代码:我添加了函数 _refreshList()
猜你喜欢
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
相关资源
最近更新 更多