WPF DataGrid选择了行样式

时间:2010-12-27 15:58:01

标签: wpf datagrid styles

我遇到了一个非常愚蠢的问题 - 需要在WPF DataGrid中设置所选行的样式。

我想要显示一个带蓝色边框的矩形,而不是只用一些颜色填充整行。

任何想法如何实现?它必须是某种方式使它变得非常容易。

3 个答案:

答案 0 :(得分:86)

CellStyle上使用RowStyleDataGridDataGridCellDataGridRow都有IsSelected属性,可以在Trigger中使用,以确定它们是否已被选中。

像下面这样的事情可以解决问题:

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Style.Triggers>
            <Trigger Property="IsSelected"
                        Value="True">
                <Setter Property="Background"
                        Value="White" />
                <Setter Property="Foreground"
                        Value="Black" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <Trigger Property="IsSelected"
                        Value="True">
                <Setter Property="BorderBrush"
                        Value="Blue" />
                <Setter Property="BorderThickness"
                        Value="2" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

只要玩得开心,直到你做对了。

答案 1 :(得分:6)

我喜欢这个:

emit -limit 1
|(
  put field1 = 1, field2 = 2;
  put field2 = 2, field3 = 3;
)| split // break each point into one point for each field, assigning each field name into the point's name field
 | reduce by name // get unique list of name field values
 | sort name
 | @logger

{"name":"field3"}
{"name":"field2"}
{"name":"field1"}
==============================================================

答案 2 :(得分:5)

试试这个

<DataGrid.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>

                    <Style TargetType="{x:Type DataGridRow}">
                        <Setter Property="HeaderStyle">
                            <Setter.Value>
                                <Style TargetType="{x:Type DataGridRowHeader}">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                    <Setter Property="Width" Value="0"/>
                                </Style>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
                        <Setter Property="SnapsToDevicePixels" Value="true"/>
                        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
                        <Setter Property="ValidationErrorTemplate">
                            <Setter.Value>
                                <ControlTemplate>
                                    <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>

                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type DataGridRow}">
                                    <Border x:Name="DGR_Border" BorderThickness="1" CornerRadius="5" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                        <SelectiveScrollingGrid>
                                            <SelectiveScrollingGrid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto"/>
                                                <ColumnDefinition Width="*"/>
                                            </SelectiveScrollingGrid.ColumnDefinitions>
                                            <Grid Grid.Column="1">
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="*"/>
                                                    <RowDefinition Height="Auto"/>
                                                </Grid.RowDefinitions>
                                                <DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                                <DataGridDetailsPresenter Margin="4" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>

                                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.RowSpan="2"/>
                                            </Grid>
                                            <DataGridRowHeader SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        </SelectiveScrollingGrid>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsSelected" Value="True">
                                            <Setter TargetName="DGR_Border" Property="BorderBrush" Value="Blue"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="DetailsVisibility" Value="Visible">
                                <Setter Property="BorderThickness" Value="4,1,4,4"/>
                                <Setter Property="BorderBrush" Value="#FF3886B9"/>
                            </Trigger>
                            <!--<Trigger Property="IsSelected" Value="True">
                                <Setter Property="BorderBrush" Value="Blue"/>
                            </Trigger>-->
                        </Style.Triggers>
                    </Style>

                </DataGrid.Resources>