get designheight/width of usercontrol at run time - wpf

Im writing a WPF application where usercontrols are added to a TabControl at runtime - creating a tab for each user control. The problem then is...these controls can have different width and height which means the tabcontrol must adjust its own width and height accordingly. I tought this would be a simple exercise of just accessing the usercontrols Height/ActualHeight properties, but these are NaN/0.0
Is it not possible to get this information?

I can propose next solution:
When you add a new control to the TabControl (is it a TabControl or a TabItem?) set bindings for the Width and Height properties. Create a converter to convert sizes of added controls to the size of Owner (in case if you need to have minimum size).
ActualWidth and ActualHeight properties perhaps are 0 because control wasn't measured yet. Look this thread

Related

Bind WPF conrol size to window size

I am trying to bind a TabControl to the size of the MainWindow in WPF. I have done it very easily, but the problem is with padding or margin.
If I just set the both with the same size during the window resize, then the TabControl hangs off the right side of the window by it's border. I have tried playing with padding and margin of the TabControl with no effect on it's over flow of the window.
Should I use a custom converter? My skill with them is pretty novice and am not sure how I would work a two way converter for these properties.

WPF Height at Runtime

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.
Why not use the property MaxHeight for the stackpanel?
Also ActualWidth and ActualHeight will tell you the actual size of a control.
Are you sure you need all these calculations? My guess is that it would be better to use auto sizing GridRows or something like that.
It sounds to me like you want a custom panel to do layout in a very specific manner. You can inherit a new class from Panel, and then override MeasureOverride and ArrangeOverride to determine what can be added and where. This is also the method you'd use to do a virtual panel, where only the visible children are created and they are destroyed when scrolled off.
If you are interested in this let me know and I'll edit the post and provide an example.

WPF - Make two controls the same size

Is there any way to make two controls that are in different containers the same size in WPF? For example, suppose you have two textboxes: textbox1 and textbox2. Textbox1 is in a grid and its size can grow and shrink when the user resizes the window. Textbox2 is in another part of the window and I need it to always have the same size as textbox1. Is there any way to do this?
Keep in mind SharedSizeGroup will not work because the textboxes are in different containers. Also, I've tried binding textbox2's height property to textbox1 and that doesn't seem to work either. Finally, I tried catching textbox1's SizeChanged event, but its Height property is always NaN for some reason.
You should bind to ActualWidth and ActualHeight members of the TextBox.

WPF UserControl Designer Problem with Strech

I have a WPF UserControl (Foo.xaml) that should be displayed streched (VerticalAlignment="Stretch" HorizontalAlignment="Stretch") on a window.
If I don't specify Width/Height on the UserControl it will be displayed as intended at runtime. Unfortunately the VS WPF designer will not show the control correctly if I open Foo.xaml (size may be 0x0 depending on the child controls).
If I specify a size on the UserControl the designer will work correctly with Foo.xaml but then it won't strech at runtime.
How can I fix this so that it works at design and runtime?
It sounds like your UserControl is only useful if it is a certain minimum size. If so, set the MinWidth and MinHeight properties.
You can set the DesignWidth and DesignHeight properties on the UserControl to adjust the height and width of the control in the design view.
You can adjust those properties with the mouse by dragging the square rectangles that show up when you select the resize tool for the UserControl.

autosize controls in TabPages

I have a TabControl consisting of several TabPages. One of these TabPages contains a TreeView and another contains a DataGridView. I would like these controls (and tabpages) to AutoSize to the maximum size allowed in the TabControl without scrolling.
The DataGridView contains an AutoSize property inherited from Control which garbles the control if enabled. The TreeView does not have this property. I have tried setting the Size equal to TabControl.Size but that does not account for bordersize and the Tabbar height.
treeView.Size = new Size(tabControl.Size.Width - SystemInformation.Border3DSize.Width * 2, tabControl.Size.Height - SystemInformation.Border3DSize.Height * 2);
My question is: how can I determine the height of the Tab buttons or how can I automatically fill TabPages to their maximum size with a single control?
The Property you search is called Dock.
You probably want to set it to DockStyle.Fill on your TreeView and DataGridView.
An alternative would be to set the Anchor Property to AnchorStyle.Top, AnchorStyle.Left, AnchorStyle.Right and AnchorStyle.Bottom (concatenate with binary OR the pipe sign '|').
For that you would need to set the size of the Children to ClientSize

Resources