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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:39:18+00:00 2026-05-20T00:39:18+00:00

Sorry if this is a dense question, but I’m looking to see if there

  • 0

Sorry if this is a dense question, but I’m looking to see if there is a way to have a style resource of Canvas in App.xaml and also have children on that Canvas and just refer to it in the Style of another canvas. The resource I envision would be something like:

<Style x:Key="Background1" TargetType="Canvas">
            <Setter Property="Width" Value="500"/>
            <Setter Property="Height" Value="600" />
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Children">
                <Setter.Value>
                    <Rectangle Canvas.Top="20" Canvas.Left="20"  Width="100" Height="100" Fill="Yellow"></Rectangle>
                </Setter.Value>
            </Setter>
        </Style>

And then calling it would be as simple as:

<Canvas Style="{StaticResource Background1}"  x:Name="CanvasRoot"  >
    <Rectangle x:Name="PageRectangle" Canvas.Left="114" Canvas.Top="84" Height="378" Width="210" Stroke="#92D050" Fill="#C0504D" />
</Canvas>

Any thoughts on if something like this can be done. What I’ve done doesn’t work because there is no “Children” property on Canvas.

  • 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-20T00:39:19+00:00Added an answer on May 20, 2026 at 12:39 am

    So you want to add a child to a Canvas via a Style? I’m afraid this is simply not possible. Styles allows you to set the values of an elements dependency properties, such as Height, Background, Stroke etc … You can also use them to set attached properties. However, the Children property you are trying to set is not a dependency property, it is the collection of children elements that describe the children of your canvas in the visual tree.

    The only way to add new elements t the visual tree using styles is to add them to some controls template. Unfortunately you canot template panels (Canvas, Grid, StackPanel). You could use a ContentControl and add your rectangle as part of its template.

    A ContentControl is a lookless container of a single child. See the template described here:

    http://msdn.microsoft.com/en-us/library/dd334411%28VS.95%29.aspx

    Here it is, templates to add a rectangle. I am not sure what layout you are trying to achieve, but it should give you the general idea

    <Style TargetType="ContentControl" x:Key="myContentControl">
          <Setter Property="Foreground" Value="#FF000000"/>
          <Setter Property="HorizontalContentAlignment" Value="Left"/>
          <Setter Property="VerticalContentAlignment" Value="Top"/>
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="ContentControl">
                      <Canvas>
                        <Rectangle Canvas.Top="20" Canvas.Left="20"  Width="100" Height="100" Fill="Yellow"></Rectangle>
                        <ContentPresenter
                            Content="{TemplateBinding Content}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            Cursor="{TemplateBinding Cursor}"
                            Margin="{TemplateBinding Padding}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                      </Canvas>
                  </ControlTemplate>
              </Setter.Value>
          </Setter>
    

    <ContentControl Style={StaticResource myContentControl}>
      <Canvas x:Name="CanvasRoot"  >
        <Rectangle x:Name="PageRectangle" Canvas.Left="114" Canvas.Top="84" Height="378" Width="210" Stroke="#92D050" Fill="#C0504D" />
      </Canvas>
    </ContentControl>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this is a stupid question, but is there an easy way to
Sorry for this simple question, but I can't solve it... There is an example:
Sorry this is probably a dumb question but I have been struggling with this
Sorry this might be a very stupid question. But I did have bug with
I'm sorry if this question has been asked. I have looked but can not
Sorry this is a total newb question but there is this really great C-library
Sorry this is a basic question, but all my research just barely missed answering
sorry this is such a simple question but I can't figure it out. How
Sorry if this question seems stupid, but it's stumped me for a couple days,
Sorry if this is a dupe. There are many similar questions but it seems

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.