0

I want to disable DataRow selection in WPF DataGrid.

First I have tried IsHitTestVisible property, it disables whole row. If I want to put an HyperLink it will be disabled.

I have tried to get rid of it by overriding DataGridRow ControlTemplate's :

(Whole template code is huge but i just put the important section)

 <VisualState x:Name="Normal_Selected">
     <Storyboard>
         <ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
             <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlMediumColor}" />
         </ColorAnimationUsingKeyFrames>
         <ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
             <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlDarkColor}" />
         </ColorAnimationUsingKeyFrames>
         <!--<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)">
             <EasingColorKeyFrame KeyTime="0" Value="Black" />
         </ColorAnimationUsingKeyFrames>-->
     </Storyboard>
 </VisualState>

Actually selecting a row in DataGrid will change Forground color. I have tried Triggers but it won't work. In the code above, I have tried to keep foreground Black but it throws in a run-time error.

How can i disable Row selection in a DataGrid ?

Kzryzstof
  • 7,688
  • 10
  • 61
  • 108
RezaNoei
  • 1,266
  • 1
  • 8
  • 24
  • If i am getting this right, you have a DG with rows containing hyper links, you want to disable row's selection but leave the links clickable ? – SamTh3D3v Jul 29 '18 at 19:56
  • Yea. In a DataGridRow we can put a lots of controls. i want to hide selection highlight actually (because it seems that i cant get rid of Selection) – RezaNoei Jul 29 '18 at 20:05
  • https://stackoverflow.com/questions/12882423/datagrid-disabling-rows-wpf – Incredible Jul 30 '18 at 12:17
  • Making rows read-only doesn't prevent to row selecting. the only way (the only way that i have found) was to make `IsHitTestVisible` to `false`. in this way all clickable control on the target control will turns to non-clickable one. but it will not solve my problem. I want to disable back-color changes in row selecting. – RezaNoei Jul 30 '18 at 17:59

1 Answers1

0

Try this, it should work very well if you are using wpf. However, it doesn't work if you are doing winui3

   <DataGrid.Resources>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="BorderBrush" Value="Transparent" />
                            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                        </Style>
                    </DataGrid.Resources>