我有一個觀點ConfigRole其中包含的DataGrid有兩列:查看和的IsEnabled(複選框)和搜索區域。覆蓋記錄搜索-MVVM WPF
和按鈕來保存它正常工作,我做我想要的IsEnabled各方意見,我保存它: 爲爲例:
當我使用搜索框,我有正確的導致所有我在上面搜索看法,供爲例我寫的「客戶」搜索框,我都用鑰匙「客戶」的看法:
我的問題當我做保存按鈕搜索後,所有的複選框(在的IsEnabled第一視圖將假!只是我在搜索視圖中啓用它的視圖是保存!
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();
公共靜態事件refreshList _refreshList =代表{};
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; }
} ...
}
`
,位於該方法的搜索,並在功能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;
}
我該如何解決它?
感謝,
檢查,如果你以前保存的視圖覆蓋「的IsEnabled」。 –
那麼你在代碼中設置ViewRoleMapClass的IsEnabled屬性? – mm8
@ mm8我已經編輯我的帖子與類和IsEnabled的小描述 – devtunis