none
Changing the color of the background of the row and text color on selection + code behind

    Question

  • Hi,

    How can I change the background color of the row and font color of the row on the selection of the item. I want to implement this is code behind.

    Regards,
    Suresh  

    Saturday, June 09, 2012 7:16 AM

Answers

  • Hi,

    You should set the style of your row when you select the row:

    <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SilverlightApplication285.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:localprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
        xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        <UserControl.Resources>
            <Style TargetType="sdk:DataGridRow" x:Key="MyDataGridRow">
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="sdk:DataGridRow">
                            <localprimitives:DataGridFrozenGrid Name="Root">
                                <vsm:VisualStateManager.VisualStateGroups>
                                    <vsm:VisualStateGroup x:Name="CommonStates">
                                        <vsm:VisualState x:Name="Normal"/>
                                        <vsm:VisualState x:Name="NormalAlternatingRow">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="0"/>
                                            </Storyboard>
                                        </vsm:VisualState>
                                        <vsm:VisualState x:Name="MouseOver">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To=".5"/>
                                            </Storyboard>
                                        </vsm:VisualState>
                                        <vsm:VisualState x:Name="NormalSelected">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                                            </Storyboard>
                                        </vsm:VisualState>
                                        <vsm:VisualState x:Name="MouseOverSelected">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                                            </Storyboard>
                                        </vsm:VisualState>
                                        <vsm:VisualState x:Name="UnfocusedSelected">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                                                <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Fill).Color" To="#FFE1E7EC"/>
                                            </Storyboard>
                                        </vsm:VisualState>
                                    </vsm:VisualStateGroup>
                                    <vsm:VisualStateGroup x:Name="ValidationStates">
                                        <vsm:VisualState x:Name="Valid"/>
                                        <vsm:VisualState x:Name="Invalid">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Visibility">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                                                </ObjectAnimationUsingKeyFrames>
                                                <DoubleAnimation Storyboard.TargetName="InvalidVisualElement" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                                            </Storyboard>
                                        </vsm:VisualState>
                                    </vsm:VisualStateGroup>
                                </vsm:VisualStateManager.VisualStateGroups>
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
    
                                <Grid.Resources>
                                    <Storyboard x:Key="DetailsVisibleTransition">
                                        <DoubleAnimation Storyboard.TargetName="DetailsPresenter" Storyboard.TargetProperty="ContentHeight" Duration="00:00:0.1" />
                                    </Storyboard>
                                </Grid.Resources>
                                <Rectangle x:Name="BackgroundRectangle" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="Red"/>
                                <Rectangle x:Name="InvalidVisualElement" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="#FFF7D8DB"/>
                                <localprimitives:DataGridRowHeader Grid.RowSpan="3" Name="RowHeader" localprimitives:DataGridFrozenGrid.IsFrozen="True" />
                                <localprimitives:DataGridCellsPresenter Grid.Column="1" Name="CellsPresenter" localprimitives:DataGridFrozenGrid.IsFrozen="True" />
                                <localprimitives:DataGridDetailsPresenter Grid.Row="1" Grid.Column="1" Name="DetailsPresenter" />
                                <Rectangle Grid.Row="2" Grid.Column="1" Name="BottomGridLine" HorizontalAlignment="Stretch" Height="1" />
                            </localprimitives:DataGridFrozenGrid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
            <sdk:DataGrid x:Name="dataGrid1" 
                Height="140" Margin="0,5,0,10"
                AutoGenerateColumns="True" RowStyle="{StaticResource MyDataGridRow}" />
        </Grid>
    </UserControl>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace SilverlightApplication285
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                dataGrid1.ItemsSource = Customer.GetSampleCustomerList();
            }
        }
        public class Customer
        {
            public String FirstName { get; set; }
            public String LastName { get; set; }
            public String Address { get; set; }
            public Boolean IsNew { get; set; }
    
            // A null value for IsSubscribed can indicate 
            // "no preference" or "no response".
            public Boolean? IsSubscribed { get; set; }
    
            public Customer(String firstName, String lastName,
                String address, Boolean isNew, Boolean? isSubscribed)
            {
                this.FirstName = firstName;
                this.LastName = lastName;
                this.Address = address;
                this.IsNew = isNew;
                this.IsSubscribed = isSubscribed;
            }
    
            public static List<Customer> GetSampleCustomerList()
            {
                return new List<Customer>(new Customer[4] {
                    new Customer("A.", "Zero", 
                        "12 North Third Street, Apartment 45", 
                        false, true), 
                    new Customer("B.", "One", 
                        "34 West Fifth Street, Apartment 67", 
                        false, false),
                    new Customer("C.", "Two", 
                        "56 East Seventh Street, Apartment 89", 
                        true, null),
                    new Customer("D.", "Three", 
                        "78 South Ninth Street, Apartment 10", 
                        true, true)
                });
            }
        }
    
    }
    


    And if you want to set the click, you should get which row you have select first and then set the cell's font color one by one.

    One example for how to get the cell content:

    http://social.msdn.microsoft.com/Forums/en-US/silverlightcontrols/thread/b032b218-3f28-4d02-8042-571f16820f1d#603dc5c6-567b-46ff-bfec-3d8933bfe62e/1?Re+I+want+to+highlight+all+the+columns+of+a+datagrid+by+mousehover+of+the+column+header+just+like+incase+of+rows+by+default+all+the+cells+of+a+particular+row+is+highlighted+

    Hope helpful

     

    Monday, June 11, 2012 1:46 AM