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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:59:44+00:00 2026-05-24T12:59:44+00:00

It works realy strange. I have some class derived from the framework element. It

  • 0

It works realy strange. I have some class derived from the framework element. It is stupid code because I want to make it easier 😛

public class Strange : FrameworkElement
{
    public DrawingVisual Visual;

    public Strange()
    {
        Visual = CreateDrawingVisualRectangle();
    }

    protected override Visual GetVisualChild(int index)
    {
        return Visual;
    }
    protected override int VisualChildrenCount
    {
        get
        {
            return 1;
        }
    }

    // Create a DrawingVisual that contains a rectangle.
    public DrawingVisual CreateDrawingVisualRectangle()
    {

        var drawingVisual = new DrawingVisual();

        DrawingContext drawingContext = drawingVisual.RenderOpen();
        var rect = new Rect(new Point(0, 0), new Size(100, 100));
        drawingContext.DrawRectangle(Brushes.Red, null, rect);
        drawingContext.Close();

        return drawingVisual;
    }
}

Next I put this class to the Grid.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Button Click="Button_Click">Change Color</Button>
    <Test:Strange Grid.Column="1" x:Name="strange"/>
</Grid> 

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        strange.Visual = strange.CreateDrawingVisualRectangle();
        var size = new Size(strange.ActualWidth, strange.ActualHeight);
        strange.Measure(size);
        strange.Arrange(new Rect(size));
        strange.UpdateLayout();
    }

When I pressed the Button, the Strange object went to the first column (it is rendered at the begining of the Grid). I have more logic in my project and I need to refresh Strange object, but it should be still in the second column (in the place where it was).

What I did wrong?

  • 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-24T12:59:46+00:00Added an answer on May 24, 2026 at 12:59 pm

    When you call Arrange on the object, it is redrawing itself at the location you provide, relative to its parent. Since its parent is the grid, (0,0,size.Width,size.Height) will put it in the upper-left hand corner.

    To fix your immediate problem, instead of measuring, and arranging, you should just call InvalidateMeasure:

    strange.Visual = strange.CreateDrawingVisualRectangle();
    strange.InvalidateMeasure();
    

    However, I think you are approaching this problem slightly wrong. I think what you want to do is to override OnRender to draw your control. It is very similar to what you’re doing with the DrawingVisual, except your FrameworkElement is what is being drawn:

    protected override void OnRender(DrawingContext dc)
    {            
        var rect = new Rect(new Point(0, 0), new Size(100, 100));
        dc.DrawRectangle(Brushes.Red, null, rect);
    }
    

    I assume that you would have some DependencyProperty’s that you are using to control how your Strange class is drawn. If you register them with the FrameworkPropertyMetadata.AffectsRender property, it will automatically cause the class to be redrawn. For instance, this will allow you to set a Color for the Strange object, and it will automatically get redrawn when you set the property:

    public class Strange : FrameworkElement
    {
        #region Color Dependency Property
        public static readonly DependencyProperty ColorProperty = 
          DependencyProperty.Register(
          "Color",
          typeof(Color),
          typeof(Strange),
          new FrameworkPropertyMetadata(Colors.Red, 
              FrameworkPropertyMetadataOptions.AffectsRender));
    
        public Color Color
        {
            get { return (Color)GetValue(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }
        #endregion 
    
        protected override void OnRender(DrawingContext dc)
        {            
            var rect = new Rect(new Point(0, 0), new Size(100, 100));
            dc.DrawRectangle(new SolidColorBrush(Color), null, rect);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found strange generics behaviour. In two words - thing I realy want is
I am using CSLA.NET. It works realy nice with the wsHttpBinding. Now, I have
A very strange error: if I add some specific code to my project, any
I have a class, A , in C++/CLI which derives from a templated base
I've encountered with some strange problem. It's strange for me because I don't understand
I have fancybox installed on my website and I am getting some strange behaviour.
I have a strange problem. I have a jQuery document.ready function executing some script
As we know in C++ we have class iostream, which is inherited from istream(basic_istream)
really strange situation I got here. I have 2 classes. @Entity public class CategoryData
I have a an HTML5 video element that plays and communicates with a some

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.