Home > DeveloperSection > Forums > WPF overrided datagrid how to set DatagridCell Selected style
E E Cummings
E E Cummings

Total Post:26

Points:182
Posted on    July-19-2013 11:50 PM

 WPF WPF 
Ratings:


 1 Reply(s)
 2689  View(s)
Rate this:

I have overrides DataGrid. Below are my code and XAML. How can I set style to change the DataGridCell background colour when the cell is selected?

<vw:DataGridExt

   Grid.Row="1"

   AlternatingRowBackground="LightGray"

   ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Value,Mode=OneWay, IsAsync=True}"

   Background="White"

   AutoGenerateColumns="True"

   IsReadOnly="True"

   CanUserResizeRows="False"

   ClipboardCopyMode="IncludeHeader"

   CanUserSortColumns="True"

   CanUserAddRows="False"

   SelectionMode="Extended"/>                

 public class DataGridExt : DataGrid

 {

     public DataGridExt()

     {

         this.AutoGeneratedColumns += new EventHandler(DataGrid_AutoGeneratedColumns);

     }

 }

thanks in advance



shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Points:735
Posted on    July-19-2013 11:57 PM

Solution!

You have to provide a style for your DataGridCell

<Style TargetType="{x:Type DataGridCell}">  

  <Setter Property="Background" Value="Transparent" />       

    <Setter Property="BorderThickness" Value="1"/>

    <Setter Property="BorderBrush" Value="Transparent"/>    

    <Setter Property="Template">

        <Setter.Value>

            <ControlTemplate TargetType="{x:Type DataGridCell}">

                <Border Background="{TemplateBinding Background}"

                        BorderBrush="{TemplateBinding BorderBrush}" 

                        BorderThickness="{TemplateBinding BorderThickness}" >

                    <ContentPresenter />

                </Border>                             

            </ControlTemplate>

        </Setter.Value>

    </Setter>

    <Style.Triggers>

        <Trigger Property="IsKeyboardFocusWithin" Value="True">

            <Setter Property="BorderBrush" Value="Black" />

        </Trigger>

        <Trigger Property="IsSelected" Value="True">

            <Setter Property="Background" Value="Gray"/>

        </Trigger>

    </Style.Triggers>

</Style>

and set

SelectionUnit="Cell" for your DataGrid.


Don't want to miss updates? Please click the below button!

Follow MindStick