【发布时间】: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();
公共静态事件 refreshList _refreshList = delegate { };
public int test;
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 服务只是我调用它..