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 6781657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:39:00+00:00 2026-05-26T16:39:00+00:00

I used an example I found online to create a custom grid control. It

  • 0

I used an example I found online to create a custom grid control. It allows me to place the kind of gridlines that I want to make a calender. But now When I go to set the background it stops my custom gridlines from displaying.

I know it’s b/c the OnRender is being called first and then the background which is getting rid of my custom settings. I tied getting rid of the override on OnRender but still no luck. So my question is how can I make a custom background that will still allow the gridlines to be shown.

This is the background I want on my custom control: If I try to add this to the custom control my gridlines disappear.

    <Grid.Background>
        <RadialGradientBrush>
            <GradientStop Color="#FFC3D6F5" Offset="0" />
            <GradientStop Color="#FFEFF5FF" Offset="1" />
        </RadialGradientBrush>
    </Grid.Background>

This is the custom control I found online. It only sets customgridline properties. I need a custom background property now. It’s a little harder because my background is not a solid color.

namespace Camp_
{    
public class GridControl : Grid
{

    #region Properties
    public bool ShowCustomGridLines
    {
        get { return (bool)GetValue(ShowCustomGridLinesProperty); }
        set { SetValue(ShowCustomGridLinesProperty, value); }
    }



    public static readonly DependencyProperty ShowCustomGridLinesProperty =
        DependencyProperty.Register("ShowCustomGridLines", typeof(bool), typeof(GridControl), new UIPropertyMetadata(false));

    public Brush GridLineBrush
    {
        get { return (Brush)GetValue(GridLineBrushProperty); }
        set { SetValue(GridLineBrushProperty, value); }
    }


    public static readonly DependencyProperty GridLineBrushProperty =
        DependencyProperty.Register("GridLineBrush", typeof(Brush), typeof(GridControl), new UIPropertyMetadata(Brushes.Black));



    public double GridLineThickness
    {
        get { return (double)GetValue(GridLineThicknessProperty); }
        set { SetValue(GridLineThicknessProperty, value); }
    }

    public static readonly DependencyProperty GridLineThicknessProperty =
        DependencyProperty.Register("GridLineThickness", typeof(double), typeof(GridControl), new UIPropertyMetadata(1.0));
    #endregion

    protected override void OnRender(DrawingContext dc)
    {  

        if (ShowCustomGridLines)
        {

            foreach (var rowDefinition in RowDefinitions)
            {
                dc.DrawLine(new Pen(GridLineBrush, GridLineThickness), new Point(0, rowDefinition.Offset), new Point(ActualWidth, rowDefinition.Offset));
            }

            foreach (var columnDefinition in ColumnDefinitions)
            {
                dc.DrawLine(new Pen(GridLineBrush, GridLineThickness), new Point(columnDefinition.Offset, 0), new Point(columnDefinition.Offset, ActualHeight));
            } 
            dc.DrawRectangle(Brushes.Transparent, new Pen(GridLineBrush, GridLineThickness), new Rect(0, 0, ActualWidth, ActualHeight));
        }
        base.OnRender(dc);
    }

    static GridControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(GridControl), new FrameworkPropertyMetadata(typeof(GridControl)));
    }
}
}

And the XAML:

<customgridcontrol:GridControl ShowCustomGridLines="True" GridLineBrush="CornflowerBlue" ShowGridLines="False" >
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
</customgridcontrol:GridControl>
  • 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-05-26T16:39:01+00:00Added an answer on May 26, 2026 at 4:39 pm

    I think you only have to set the standard background property in the xaml code like this:

    <customgridcontrol:GridControl ShowCustomGridLines="True" GridLineBrush="CornflowerBlue" ShowGridLines="False" Background="Binding{ StaticResource BrushYouWantTo" >
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
    </customgridcontrol:GridControl>
    

    The “brush you want to” must set as resource in the xaml code like this:

    <Window.Resources>
       <RadialGradientBrush Name="BrushYouWantTo">
          <GradientStop Color="#FFC3D6F5" Offset="0" />
          <GradientStop Color="#FFEFF5FF" Offset="1" />
       </RadialGradientBrush>
    </Window.Resources>
    

    But if you want to change the background color while the program is running you need a dependency property and a event handler who reload the gui. To use this event handler you must define it with a

    new UIPropertyMetadata(CustomGridBackgroundChanged)
    

    And the event handler definition can look like this:

      private static void CustomGridBackgroundChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
      {
         WindowName win = (WindowName)sender;
         if (win.PropertyChanged != null)
         {
            // at this place the gui will be reloaded
            win.PropertyChanged(win, new PropertyChangedEventArgs(null));
         }
      }
    

    It is only an idea so i dont really know if it work in practice…
    But i hope it help you a little bit

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this example and used it How do you resize a Bitmap under
For example DbVisualizer can be used to connect to a DB and create nice
I know from wikipedia for example that exception handling is used in an application
I found a great control with example here for mvc It fulfills all my
I found this example online and it was perfectly fine but I'd like to
Consider this classic example used to explain what not to do with forward declarations:
Could you tell me some http-streaming tutorial or example ( used also by Gmail
Example: If I used this, where does the iPhone store the file? if (![NSKeyedArchiver
For example: @interface Fraction: NSObject { ... When wouldn't NSObject be used and is
anyone has a working example of a TTThumbsViewController used with files included in the

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.