wpf controls - WPF dependecy property changed ActualWidth == 0 -


how force window measure controls in constructor values of actualwidth , actualheight aren't zero? here sample demonstrating problem (i tried call measure , arrange functions, maybe in wrong manner).

xaml:

<window x:class="wpfapplication7.window1"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         windowstartuplocation="centerscreen"         title="wpf diagram designer"         background="#303030"         height="600" width="880" x:name="root">    <grid x:name="layoutroot">         <dockpanel>             <textbox dockpanel.dock="top" text="{binding elementname=root, mode=twoway, path=count}"/>             <button dockpanel.dock="top" content="xxx"/>             <canvas x:name="maincanvas">              </canvas>         </dockpanel>     </grid> </window> 

code behind:

using system.windows; using system.windows.controls; using system.windows.shapes; using system; using system.windows.media;  namespace wpfapplication7 {     public partial class window1 : window     {         public window1()         {             initializecomponent();             measure(new size(double.positiveinfinity, double.positiveinfinity));             arrange(new rect(desiredsize));             count = 6;         }          public static readonly dependencyproperty countproperty = dependencyproperty.register("count",             typeof(int), typeof(window1), new frameworkpropertymetadata(5, countchanged, coercecount));          private static object coercecount(dependencyobject d, object basevalue)         {             if ((int)basevalue < 2) basevalue = 2;             return basevalue;         }          public int count         {             { return (int)getvalue(countproperty); }             set { setvalue(countproperty, value); }         }          private static void countchanged(dependencyobject d, dependencypropertychangedeventargs e)         {             window1 w = d window1;             if (w == null) return;             canvas c = w.maincanvas;             if (c == null || c.children == null) return;             c.children.clear();             if (c.actualwidth == 0) messagebox.show("xxx");             (int = 0; < w.count; i++)                 c.children.add(new line()                 {                     x1 = c.actualwidth * / (w.count - 1),                     x2 = c.actualwidth * / (w.count - 1),                     y1 = 0,                     y2 = c.actualheight,                     stroke = brushes.red,                     strokethickness = 2.0                 });         }     } } 

the point of example have drawn count number of vertical lines left edge right edge. works when change value in textbox, want lines drawn in beginning already.

so how need update code have drawn lines in beginning please? or different approach mentioned code more appropriate achieve goal?

thanks efforts.

perhaps put logic in oncontentrendered.

the windows content should layed out , actualwidth etc should ready.

example:

protected override void oncontentrendered(eventargs e) {     base.oncontentrendered(e);      // logic } 

Comments