Поля в ячейке данных WPF не активируются - PullRequest
0 голосов
/ 05 марта 2020

Я создал собственный стиль для WPF DataGrid.

Одна вещь состояла в том, чтобы добавить поле для каждой ячейки.
Работает нормально, но вы не можете выбрать строку, когда нажимаете на margin.
Понятия не имею, как это обойти.

<DataGrid ItemsSource="{Binding Items}">
   <DataGrid.CellStyle>
       <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <!-- Here is the margin for every cell -->
                        <ContentPresenter Margin="20"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
   </DataGrid.CellStyle>

   <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="BorderThickness" Value="0 1 0 1"/>
            <Setter Property="BorderBrush" Value="{x:Null}"/>
            <Style.Triggers>
                <!-- mouseover works fine, even for the margin -->
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderBrush" Value="Orange"/>
                </Trigger>
                <!-- this gets not set when you click on the margin in the cell, so you think when the row highlightes you also can select it, but you cant -->
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
         </Style>
   </DataGrid.RowStyle>           
</DataGrid>

1 Ответ

2 голосов
/ 05 марта 2020

Margin="20" создает пустую область вокруг содержимого ячейки, которая не кликабельна, потому что не заполнена ничем. Самое простое решение - добавить границу с прозрачным фоном (см. Связанный QA {x: Null} против прозрачного bru sh)

<ControlTemplate TargetType="{x:Type DataGridCell}">
    <!-- Here ist the margin for every cell -->
    <Border Background="Transparent">
        <ContentPresenter Margin="20"/>
    </Border>
</ControlTemplate>
...