user2088807
user2088807

Reputation: 1408

Set a border on a datagrid row

I've the following styles for my datagrid rows and cells :

<Style TargetType="{x:Type DataGridRow}">
            <Setter Property="Margin" Value="0"/>
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="Foreground" Value="{StaticResource ResourceKey=CouleurTexte}" />
            <Setter Property="Padding" Value="0"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Cursor" Value="Arrow"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{StaticResource ResourceKey=CouleurDataGridRowSelected}"/>
                    <Setter Property="Foreground" Value="{StaticResource ResourceKey=CouleurTexteBouton}" />
                    <Setter Property="BorderBrush" Value="{StaticResource ResourceKey=CouleurDataGridBorderSelected}" />
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="{StaticResource ResourceKey=CouleurDataGridRowHover}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
<Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Padding" Value="5" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Cursor" Value="Arrow"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                            <ContentPresenter.ContentTemplate>
                                <DataTemplate>
                                    <TextBlock Background="Transparent" Name="texte" TextTrimming="CharacterEllipsis" 
                                Height="auto" Width="auto" VerticalAlignment="Center" Text="{Binding Text}"/>
                                </DataTemplate>
                            </ContentPresenter.ContentTemplate>
                        </ContentPresenter>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{StaticResource ResourceKey=CouleurDataGridRowSelected}"/>
            </Trigger>
            <DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True">
            </DataTrigger>
        </Style.Triggers>
    </Style>

with my datagrid defined with stars in the width of my columns (so they can take all the space of the window).

I want to set a border around a full row when its selected. My styles work fine until I set BorderThickness to 1 in the datagrid row. When I do the datagrid show the horizontal scrollbar to scroll only one (maybe 2) pixel (the one from the border added). When I don't have all my columns with "" as width it works fine but I need to find a way to make it work with "". How can I avoid this behavior ? It seems the border come on the outside of the row ?

Thank you

Upvotes: 0

Views: 1828

Answers (2)

user2088807
user2088807

Reputation: 1408

I've found a trick to do it thanks to Muds' answer.

With his solution I had troubles with alternate row background, So I've checked using Snoop and tried to change different values and it appears that we can "solve" this issue by setting a new template for the datagridRow with margin="-1,0,-1,0" (my border thickness is 1) on the DataGridCellsPresenter.

Upvotes: 0

Muds
Muds

Reputation: 4116

I remember having this issue and spending a good lot of time to fix it, lastly had to fix it by having a -ve margin on you control, You can set a -ve margin equal to your border width and see the unwanted scrollbar going away.

Upvotes: 1

Related Questions