单击复选框时WPF Datagrid行 Flink

f87krz0w  于 2023-10-22  发布在  Flink
关注(0)|答案(2)|浏览(247)

我正在做一个WPF项目,我有一个自定义的数据网格。问题是,每当我点击复选框时,整行都被 Flink (如图所示)。它只是 Flink 了1秒钟,并返回到正常的白色背景。有办法让它失效吗?

XAML代码:

<DataGrid
        ItemsSource="{Binding PracticeCheckList}"
        Width="270"
        AutoGenerateColumns="False"
        CanUserAddRows="False"
        HeadersVisibility="None"
        HorizontalAlignment="Left"
        Background="Transparent"
        GridLinesVisibility="None"
        IsReadOnly="True"
        SelectionUnit="CellOrRowHeader"
        PreviewMouseDown="DataGrid_Popup_PreviewMouseDown">
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="BorderBrush" Value="Transparent" />
        </Style>
    </DataGrid.RowHeaderStyle>

    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Margin" Value="0,6"/>
        </Style>
    </DataGrid.RowStyle>

    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Margin" Value="0,6" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="BorderBrush" Value="Transparent"/>
        </Style>
    </DataGrid.CellStyle>

    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Item">
            <DataGridTemplateColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="BorderBrush" Value="Transparent"/>
                    <Setter Property="BorderThickness" Value="0"/>
                </Style>
            </DataGridTemplateColumn.CellStyle>

            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="24,0" Background="Transparent">
                        <CheckBox                                                                        
                                Command="{Binding DataContext.CheckUncheckCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                CommandParameter="{Binding}"
                                Content=""
                                IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        <TextBlock
                                 Margin="8,2,0,0"
                                    HorizontalAlignment="Stretch"
                                    VerticalAlignment="Center"
                                    Foreground="#84878C"
                                    Text="{Binding Name}"
                                    TextWrapping="Wrap" />
                    </StackPanel>

                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
dkqlctbz

dkqlctbz1#

在自定义DataGrid中单击复选框时,整行 Flink 或突出显示可能是由于DataGrid的默认行选择行为。当您单击行中的某个元素(例如复选框)时,该行通常会被选中,这会导致您看到的突出显示效果。
试试这个:
若要禁用此行为并防止在单击复选框时突出显示该行,可以通过将包含复选框的特定行的IsRowSelected属性设置为False来修改DataGrid的XAML。您可以使用样式触发器来完成此操作。下面是一个如何实现此目标的示例:

<DataGrid ...>
<!-- Your existing DataGrid settings -->

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Margin" Value="0,6"/>
        <Style.Triggers>
            <!-- Disable row selection for rows with a CheckBox -->
            <DataTrigger Binding="{Binding Path=IsChecked}" Value="True">
                <Setter Property="IsSelected" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

<!-- Your other DataGrid settings -->
zzoitvuj

zzoitvuj2#

我又尝试了几次,找到了一种方法来禁用复选框单元格的高亮显示。下面是对我有用的代码

<DataGridTemplateColumn.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Foreground" Value="Transparent" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{x:Null}" />
                <Setter Property="BorderBrush" Value="{x:Null}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGridTemplateColumn.CellStyle>

相关问题