I have to implement a WPF DataGrid with which it is possible to select only cells in one row. How could I achieve this? I know the properties SelectionUnit
and SelectionMode
but each combination of these two properties did not lead to success.
XAML:
<DataGrid AutoGenerateColumns="True" CanUserAddRows="False"
ItemsSource="{Binding DPGridCR}" HeadersVisibility="None" SelectionUnit="Cell"
SelectionMode="Extended" CanUserResizeRows="False" />
At this moment I am just able to select many cells in many rows or just select one single cell or select a whole row. But I want to select many cells in ONE row.
Edit:
<UserControl x:Class="DesignerPro.Controls.DZLeerformularGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ViewModel="clr-namespace:ViewModel;assembly=ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="DZLeerformularGridControl">
<UserControl.Resources>
<ViewModel:DZLeerformularGridViewModel x:Key="ViewModel" />
</UserControl.Resources>
<DataGrid AutoGenerateColumns="True" CanUserAddRows="False"
ItemsSource="{Binding DPGridCR}" HeadersVisibility="None" SelectionUnit="Cell"
SelectionMode="Extended" CanUserResizeRows="False">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedCellsChanged">
<ei:CallMethodAction TargetObject="{Binding Mode=OneWay, Source={StaticResource ViewModel}}" MethodName="DataGrid_SelectedCellsChanged" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</UserControl>
With the EventTrigger
I tried to bind the SelectedCellsChanged
-Event to my ViewModel. Your solution works fine in the code-behind but in my ViewModel it doesn't work :(
SelectionUnit
andSelectionMode
combinations. – Patrick Vogt