Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:30:50+00:00 2026-06-12T00:30:50+00:00

I have two grids inside a grid. <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions>

  • 0

I have two grids inside a grid.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid x:Name="grid1" MouseLeftButtonDown="Grid_MouseLeftButtonDown" Grid.Column="0">
        <TextBlock  FontSize="50" Text="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
    <Grid x:Name="grid2"  MouseLeftButtonDown="Grid_MouseLeftButtonDown" Grid.Column="1">
        <TextBlock FontSize="50" Text="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Grid>

I am using this method for adding style to grids in runtime.

private void StyleGrid()
{
    var converter = new System.Windows.Media.BrushConverter();

    Style myS = new System.Windows.Style();

    Setter setter1 = new Setter();
    setter1.Property = Grid.BackgroundProperty;
    setter1.Value = (Brush)converter.ConvertFromString("#3F3F46");
    myS.Setters.Add(setter1);


    Setter setter = new Setter();
    setter.Property = Grid.BackgroundProperty;
    setter.Value = (Brush)converter.ConvertFromString("#54545C");

    Trigger trigger = new Trigger();
    trigger.Property = Grid.IsMouseOverProperty;
    trigger.Value = true;
    trigger.Setters.Add(setter);
    myS.Triggers.Add(trigger);

    grid1.Style = myS;
    grid2.Style = myS;
}

I am calling this method after InitializeComponent method. Everything works fine.

And this is MouseLeftButtonDown event for grids.

     private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Grid senderGr = sender as Grid;

    StyleGrid();
    var converter = new System.Windows.Media.BrushConverter();

    var brush = (Brush)converter.ConvertFromString("#007ACC");


    senderGr.Background = brush;
}

This method must change sender grid’s background color and must change style of other grid. But it is not changing style.

You can download and test project from SkyDrive if you have Microsoft account. Thanks.
https://skydrive.live.com/redir?resid=89FD450CC0CC74DA!168

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:30:52+00:00Added an answer on June 12, 2026 at 12:30 am

    Setting the Background directly will always override the defined Style. WPF uses a set order in which it determines the actual value of a Dependency Property (see http://msdn.microsoft.com/en-us/library/ms743230.aspx)

    To achieve your result just clear the set values in your StyleGrid() method:

    private void StyleGrid()
    {
        grid1.ClearValue(Grid.BackgroundProperty);
        grid2.ClearValue(Grid.BackgroundProperty);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Grids inside a Stackpanel. The first grid is named as GridX.
I have Grid inside ListPicker full mode item. Grid has two columns. First column
I have a grid with two columns with width 3* and 1*. Inside the
Inside my XAML I have two custom components: <gui:CustomerBrowser x:Name=browser CustomerDetailView=??? VerticalAlignment=Stretch HorizontalAlignment=Stretch />
I am a newbie to MVVM. I have two grid's inside by main window
I have two grids inside a TabItem and in the code-behind I want to
I have two Grid View gridview1 and gridview2 inside an asp.net page. In order
I have two grids with same number of rows ( also same height). Both
I have two grids, thay both have one store. I need to show everything
In .net objects like DataTable when you have two grids(Master and Detail) and add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.