I want to prevent that user can select another item in the list controlled by a boolean property in my view model. How do I achieve this?
Hello,
In order to control the selection type for the rows of the XamGrid by using a boolean property in the ViewModel class, I can suggest you use the XamGrid.SelectionSettings.RowSelection property by binding it for the boolean property with a converter. The converter will be used for converting the boolean values - true, false and null to SelectionType values - Multiple, Single, None.
This way whenever the boolean property gets changed, the RowSelection for the XamGrid will change accordingly.
ViewModel:
public bool? AllowMultipleSelection { get; set; }
XAML:
<ig:XamGrid.SelectionSettings> <ig:SelectionSettings RowSelection="{Binding Source={StaticResource viewModel}, Path=AllowMultipleSelection, Converter={StaticResource converter}}" /></ig:XamGrid.SelectionSettings>
Code-behind:
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture){ var allowMultipleSelection = value as bool?; switch(allowMultipleSelection) { case true: return SelectionType.Multiple; case false: return SelectionType.Single; case null: return SelectionType.None; default: return SelectionType.None; } }
I have attached a sample application that demonstrates the approach from above.
If you have any questions, please let me know.
Thanks Tacho for your answer but I think I did not explained clearly what I want to achieve.
I do not want to switch between single or multiple selection. The selection type should always be SelectionType.Single. If the boolean property is set to true, the user should be able to select another (not an additional) item. But if the boolean property is set to false, the user shall not be able to change the selected item.
Thank you for the behavior description you have provided in regards to the selection logic.
In order to prevent the selection row when the boolean property returns false, you can handle the PreviewMouseDown events for the CellControl and the RowSelectorCellControl elements. This way you can set the e.Handled property to true and any further selection logic will be prevented.
private void RowSelectorCellControl_PreviewMouseDown(object sender, MouseButtonEventArgs e){ var vm = container.Resources["viewModel"] as ViewModel.ViewModel; if (!vm.AllowSelectAnother) e.Handled = true;}
private void CellControl_PreviewMouseDown(object sender, MouseButtonEventArgs e){ var vm = container.Resources["viewModel"] as ViewModel.ViewModel; if (!vm.AllowSelectAnother) e.Handled = true;}
This solution works. Unfortunetely it is necessary to register the CellControl_PreviewMouseDown event in the view and implement it in the code behind which is not nice as it cannot be tested and has to be implemented in every view again.
Since the functionality of preventing the selection of a different row from the already selected one is not a built-in feature of the XamGrid, you can always submit a new product idea at http://ideas.infragistics.com.
Submitting your idea will allow you to communicate directly with our product management team, track the progress of your idea at any time, see how many votes it got, read comments from other developers in the community, and see if someone from the product team has additional questions for you.
Remember when submitting your idea to explain the context in which a feature would be used and why it is needed as well as anything that would prevent you from accomplishing this today. You can even add screenshots to build a stronger case. Remember that for your suggestion to be successful, you need other members of the community to vote for it. You can also link back to this thread for additional details.