gpt4 book ai didi

c# - 搜索后覆盖记录 -MVVM WPF

转载 作者:行者123 更新时间:2023-12-03 10:26:22 24 4
gpt4 key购买 nike

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

并且按钮保存它正常工作,我制作了我想要 IsEnabled 的所有 View 并保存它:
举个例子:

enter image description here

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

enter image description here

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

enter image description here

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

配置角色 View 模型:
private ObservableCollection<ViewRoleMapClass> _viewList;
private MiniTasServicesClient WCFclient = new MiniTasServicesClient();

public int test;
公共(public)静态事件 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;
}

我该如何解决?

谢谢,

最佳答案

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

我看到您正在重新初始化 _viewList在上述方法的第二行中收集。这样做可能会丢失从 View 中保存的内容。我知道您需要数据,但我认为您需要 ObservableCollection<ViewRoleMapClass> 的属性只是为了绑定(bind),类似于以下内容:

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

我希望我说清楚了。

祝你今天过得愉快

关于c# - 搜索后覆盖记录 -MVVM WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44585626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com