Answered WPF Datagrid background

  • Tuesday, May 15, 2012 5:07 PM
     
     

    Hi,

    How to change background color of Left Top Corner of datagrid. I tried to give color to  whole datagrid but still same.

    Thanks

    Dee



    • Edited by Dee Choksi Tuesday, May 15, 2012 5:07 PM
    • Edited by Dee Choksi Tuesday, May 15, 2012 5:08 PM
    •  

All Replies

  • Tuesday, May 15, 2012 7:53 PM
     
     Answered Has Code

    I used Expression to generate a style to use for your datagrid.

    The control that you will want to deal with is the <Button Command="{x:Static DataGrid.SelectAllCommand}" line where you can change the information for that button and it will allow you to change the background color.

    The following is the required Style information which can put into your window.resources and then apply to your datagrid.

        <Window.Resources>
            <Style x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}" TargetType="{x:Type Button}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Grid>
                                <Rectangle x:Name="Border" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" SnapsToDevicePixels="True"/>
                                <Polygon x:Name="Arrow" Fill="Black" HorizontalAlignment="Right" Margin="8,8,3,3" Opacity="0.15" Points="0,10 10,10 10,0" Stretch="Uniform" VerticalAlignment="Bottom"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Stroke" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                                </Trigger>
                                <Trigger Property="IsPressed" Value="True">
                                    <Setter Property="Fill" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Visibility" TargetName="Arrow" Value="Collapsed"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="DataGridStyle1" TargetType="{x:Type DataGrid}">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                <Setter Property="BorderBrush" Value="#FF688CAF"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
                <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
                <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
                <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGrid}">
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                                <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                                    <ScrollViewer.Template>
                                        <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto"/>
                                                    <ColumnDefinition Width="*"/>
                                                    <ColumnDefinition Width="Auto"/>
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="Auto"/>
                                                    <RowDefinition Height="*"/>
                                                    <RowDefinition Height="Auto"/>
                                                </Grid.RowDefinitions>
                                                <Button Command="{x:Static DataGrid.SelectAllCommand}" 
                                                        Focusable="false"
                                                        Background="Red" 
                                                        Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">
                                                        Test
                                                </Button>
                                                <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                                <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="2" Grid.Row="1"/>
                                                <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                                <Grid Grid.Column="1" Grid.Row="2">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                                        <ColumnDefinition Width="*"/>
                                                    </Grid.ColumnDefinitions>
                                                    <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                                </Grid>
                                            </Grid>
                                        </ControlTemplate>
                                    </ScrollViewer.Template>
                                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </ScrollViewer>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
    
        </Window.Resources>
        <Grid>
            <DataGrid Style="{StaticResource DataGridStyle1}" AutoGenerateColumns="True" Name="tstGrid" CanUserAddRows="True" CanUserReorderColumns="False" />
        </Grid>
    

    As you can see I have set the background to Red.  

    I removed the "normal" style for the button to allow the color change.

    Hope this helps

    Lloyd Sheen


    Lloyd Sheen

  • Tuesday, May 15, 2012 8:49 PM
     
      Has Code

    SQLGUY answers a very frequesnt question on the DataGrid.  The Button has no name but is bound to the SelectAll Command.  Therefore it is the DataGrid SelectAll Button. 

    If you force the VerticalScrollBars to appear you will also have a little square on the right side too.  You adjust that here:

     <Style x:Key="DataGridStyle1" TargetType="{x:Type DataGrid}">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}"/>

    JP Cowboy Coders Unite!

  • Wednesday, May 16, 2012 6:17 PM
     
     

    hi,

    I also want to give border to right side and bottom side attached to Port 01 and L1, how would I do it with border

    Thanks

    Dee


    • Edited by Dee Choksi Wednesday, May 16, 2012 7:56 PM
    •  
  • Wednesday, May 16, 2012 10:03 PM
     
     

    Not entirely sure what you mean but if you are referring to a datagrid cell you would have to handle it with for that cell.

    LS


    Lloyd Sheen

  • Friday, June 08, 2012 3:48 AM
    Moderator
     
     

    Hi Dee,

    I will close this thread as your original question has already been answered. For your last question, you can consider to open a new thread if you cannot fix it, and we community members will glad to help on it.

    Thanks,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us