Determining size of "autosized" user control

Given a user control that contains some kind of dynamic content (content 
I do not know the dimensions of at compilation time).  This user control 
is then dropped into a Window, without specifying size.  I am trying to 
determine the size of the content at runtime once it is defined.  Take 
the following sample code:

/// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            MyUserControl ctrl = new MyUserControl();
            ctrl.HorizontalAlignment = 
System.Windows.HorizontalAlignment.Left;
            ctrl.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            Content = ctrl;
        }
    }


public class MyUserControl : UserControl
    {
        private Canvas canvas;

        /// <summary>
        /// Initializes a new instance of the <see 
cref="T:System.Windows.Controls.UserControl"/> class.
        /// </summary>
        public MyUserControl()
        {
            canvas = new Canvas();
            Content = canvas;

            Path path = new Path();
            PathGeometry pathGeometry = new PathGeometry();
            path.Data = pathGeometry;

            PathFigure pathFigure = new PathFigure();
            pathGeometry.Figures.Add(pathFigure);
            pathFigure.StartPoint = new Point(0, 0);
            pathFigure.Segments.Add(new LineSegment(new Point(10, 10), 
true));

            path.Stroke = new SolidColorBrush(Colors.Black);
            canvas.Children.Add(path);
            Loaded += MyUserControl_Loaded;

        }

        void MyUserControl_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }

When I run this, I see a small black diagonal line appear, starting from 
the top left corner of the window - as expected.

I debugged this and set a breakpoint in the  "Loaded" event handler on 
the MyUserControl.  I then examine the following values in the Debugger 
Watch window:
Width: NaN
ActualWidth: 0.0
canvas.Width: NaN
canvas.ActualWidth: 0.0

What I was "expecting" to get back was a value of 10.  Where did my 
expectation go wrong??

In this simple scenario, I could use pathGeometry.Bounds, however in my 
"real" application, at the location I need the dimensions, all I have 
access to is the "canvas" object defined by "MyUserControl" (I need to 
have access it in the base class, but the Path object is actually setup 
by a subclass - the base class knows NOTHING about what is actually in 
canvas).

-Adam
0
Adam
12/17/2009 3:15:25 AM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

0 Replies
1491 Views

Similar Articles

[PageSpeed] 0

Reply:

Similar Artilces:

Determining size of "autosized" user control
Given a user control that contains some kind of dynamic content (content I do not know the dimensions of at compilation time). This user control is then dropped into a Window, without specifying size. I am trying to determine the size of the content at runtime once it is defined. Take the following sample code: /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); MyUserControl ctrl = new MyUserControl(); ...