I am new to WPF. A Problem I am trying to solve is how I can get a correct height at runtime.
In my Application I dynamically add Usercontrols to a Stackpanel in the Code behind. The Usercontrol contains a few Texblocks. My Stackpanel is then used as Content for a BookPage and this BookPage is added to a Book(http://wpfbookcontrol.codeplex.com/). The Height of my Stackpanel should not exceed a certain value.
I already figured out that I can use Measure & Arrange to calculate the ActualSize & Height of the Usercontrol:
itemsa.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
itemsa.Arrange(new Rect(0, 0, 400, itemsa.DesiredSize.Height));
At this point the Usercontrol isn’t added to the Stackpanel. 400 is the Width my usercontrol shouldn’t exceed, but it does, because the Textblock dosn’t create automatic linebreaks. When I display the Book the linebreaks are created.
What should I do to solve this Problem?
Thanks in advance.
Not sure if you’re looking for this, but with the MaxHeight property you have the possibility to restrict the growing of a control.
In general, it’s not necessary to override Measure and Arrange, the Layout-System is very powerfull and it offers you many container-controls that provide specific layout behaviour.