Unable to select Silverlight Datagrid cells

I have a DataGrid that I bind to a PagedCollectionView that is grouped and sorted. The contents of the DataGrid are not editable, although there is a click link in one column.

I limited the SelectionMode DataGrid to DataGridSelectionMode.Single, which stops the selection of multiple rows, which is good. But the selected row also has a selected cell, which draws a little lighter than the rest of the selected row and has a border.

Basically, I would like to have SelectedRow, but not SelectedCell (in terms of UI / Display).

It seems like this should be a simple question about setting the property, but it seems to me that I need to change the DataGrids template and / or damage the VisualStateManager.

I am happy to switch to another control other than DataGrid, but I need to display Grouping.

+3
source share
2 answers

I found a way to 'a' to make individual cells visible not selected, although I'm not sure if this is the best way.
Edit CellStyle for DataGrid, find the rectangle named FocusVisual. This is the rectangle that is used to indicate the selected cell. Set the Fill and Stroke option to Transparent, I also set the StrokeThickness to 0. Do not delete the entire rectangle because other things are waiting for it to be present. Xaml looked something like this:

    <Style x:Key="NonSelectableDataGridCellStyle" TargetType="data:DataGridCell">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="data:DataGridCell">
                    <Grid x:Name="Root" Background="{TemplateBinding Background}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CurrentStates">
                                <VisualState x:Name="Regular"/>
                                <VisualState x:Name="Current">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" 
                                            Storyboard.TargetName="FocusVisual" 
                                            Storyboard.TargetProperty="Opacity" To="1"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="ValidationStates">
                                <VisualState x:Name="Valid"/>
                                <VisualState x:Name="Invalid">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="InvalidVisualElement" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="FocusVisual" Storyboard.TargetProperty="(Fill).Color" To="#FFFFFFFF"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle x:Name="FocusVisual" 
                            Fill="Transparent"
                            Stroke="Transparent"
                            StrokeThickness="0" 
                            HorizontalAlignment="Stretch" 
                            VerticalAlignment="Stretch" 
                            IsHitTestVisible="false" 
                            Opacity="0"
                                   />
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                        <Rectangle x:Name="InvalidVisualElement" Stroke="#FFDC000C" StrokeThickness="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsHitTestVisible="False" Opacity="0"/>
                        <Rectangle x:Name="RightGridLine" VerticalAlignment="Stretch" Width="1" Grid.Column="1"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

CellStyle DataGrid

<data:DataGrid x:Name="uiDataGrid" 
           CellStyle="{StaticResource NonSelectableDataGridCellStyle}"
           >
     ...
</data:DataGrid>
+6

"":

    <sdk:DataGrid.Columns>
    <sdk:DataGridTextColumn Binding="{Binding Path=Nothing}" MinWidth="0" MaxWidth="0" />

:

Private Sub _CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.CurrentCellChanged
    If Me.CurrentColumn IsNot Nothing Then Me.CurrentColumn = Me.Columns(0)
End Sub

.

+2

Source: https://habr.com/ru/post/1721465/


All Articles