Hi,
I have a tablecontrol and into tabitem is a datagrid. Look at the attached pic, I can change the tab even when datagrid cell does not fullfiled, here as you can in the pic is the column password.
How can I prevent user to change tab, when the cell in the datagrid is not fullfiled.
Thanks for help
Hi Jean-Pascal,
Let me know if you have any further questions on this matter.
You will need to force edit mode in the XamDataGrid to complete before you can access the newly added record values. If your tabs only contain one XamDataGrid then you can use this code to get the grid and force the changes. Add it at the top of your SelectionChanged event.
XamDataGrid grid = (XamDataGrid)Utilities.GetDescendantFromType(xamTabControl1, typeof(XamDataGrid), false); if (grid != null) grid.ExecuteCommand(DataPresenterCommands.EndEditModeAndAcceptChanges);
Look at pic that I attached and the code snippet below.
public partial class LoginUsersControl : UserControl { public LoginViewModel LoginViewModel = new LoginViewModel("NasServer.xml"); //public Regex _onlyAlphaNum = new Regex("^[a-zA-Z0-9]*$"); //public bool _allowTabChange = true; public LoginUsersControl() { InitializeComponent(); DataContext = LoginViewModel; _xamTabControl.ExecuteCommand(TabControlCommands.SelectFirstTab); } private void DataPresenterBase_OnRecordActivated(object sender, RecordDeactivatingEventArgs e) { var datarecord = (DataRecord) e.Record; foreach (Field field in datarecord.FieldLayout.Fields) { CellValuePresenter cellValue = CellValuePresenter.FromRecordAndField(datarecord, field); if (cellValue.Value == null) { cellValue.StartEditMode(); e.Cancel = true; break; } } } private void _xamTabControl_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.RemovedItems.Count == 0) { return; } var login = (ServersLogins) e.RemovedItems[0]; Debug.WriteLine(login.Server); foreach (var log in login.Logins) { Debug.WriteLine(log.User); Debug.WriteLine(log.Password); } } }}
As you can see on the pic, the cell is not fullfiled and I click on tabitemex, this raised the event _xamTabControl_OnSelectionChanged. As the output I've got only
mibaztest1testtest2test
but sdsd... is missing, because after entered into cell, I did not confirm with enter key.
How can I solve my problem?
I've forgot to post my xaml code
<igWindows:XamTabControl Name="_xamTabControl" ItemsSource="{Binding AutoLogins}" Theme="Metro" AllowTabClosing="False" TabItemCloseButtonVisibility="WhenSelectedOrHotTracked" SelectionChanged="_xamTabControl_OnSelectionChanged"> <igWindows:XamTabControl.ItemContainerStyle> <Style TargetType="{x:Type igWindows:TabItemEx}" BasedOn="{x:Static igThemes:PrimitivesMetro.TabItemEx}"> <Setter Property="Header" Value="{Binding Server}" /> </Style> </igWindows:XamTabControl.ItemContainerStyle> <igWindows:XamTabControl.ContentTemplate> <DataTemplate> <igDp:XamDataGrid DataSource="{Binding Logins}" IsGroupByAreaExpanded="False" GroupByAreaLocation="None" GroupByAreaMode="DefaultFieldLayoutOnly" RecordDeactivating="DataPresenterBase_OnRecordActivated"> <igDp:XamDataGrid.FieldLayoutSettings> <igDp:FieldLayoutSettings AddNewRecordLocation="OnBottom" AllowAddNew="True" AllowDelete="True" /> </igDp:XamDataGrid.FieldLayoutSettings> <igDp:XamDataGrid.FieldSettings> <igDp:FieldSettings AllowResize="False" LabelWidth="200" LabelTextAlignment="Center"/> </igDp:XamDataGrid.FieldSettings> </igDp:XamDataGrid> </DataTemplate> </igWindows:XamTabControl.ContentTemplate> </igWindows:XamTabControl>
As you can see, I build my TabItemEx dynamic. When the cell data is not fullfilled, for example just password column is filled, so I want to prevent that user can not click on TabItemEx to change tab.
You can handle the SelectionChanged event on the XamTabControl and inside this event, if your grid data is not valid, you can set the SelectedIndex back to the index of your current tab. This will keep the selection from moving to the new tab.
private void XamTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { // make sure that both AddedItems and RemovedItems have data. If they don't the user did not select // a new tab. Also make sure we're not already updating the SelectedIndex if (e.AddedItems.Count == 0 || e.RemovedItems.Count == 0 || updatingIndex) return; if (IsGridDataValid()) { TabItemEx newTab = e.AddedItems[0] as TabItemEx; TabItemEx oldTab = e.RemovedItems[0] as TabItemEx; updatingIndex = true; xamTabControl1.SelectedIndex = xamTabControl1.Items.IndexOf(oldTab); updatingIndex = false; } }