WPF Height at Runtime - wpf

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.

Related

get designheight/width of usercontrol at run time

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

What WPF control should I inherit from?

I've written a WPF control which accepts a number of UIElement objects as input and displays them docked either vertically or horizontally. The control exposes functions for enumerating, removing and inserting children, but internally I'm using a Grid to build the layout, creating a row/column for each item and inserting a GridSplitter between them. To do this I've inherited from ContentControl, and upon initialization I just set the Content property with the Grid. Everything is working as intended, but now I wonder if this might be confusing for the user of my control, as it would be counter-intuitive to have a ContentControl that has many items.
Should I be inheriting from ItemsControl instead?
Should I inherit directly from Control which is "content-agnostic"?
Is there a better way to do this?
Thanks in advance.
What you describe sounds like a Panel - basically a control which is responsible for the layout of many elements. I would consider inheriting from that.
Your mention of "docked either vertically or horizontally" leads me to believe perhaps a StackPanel would be a better fit, since it does docking as well.

Can we overlay a WPF element by another?

I've this situation:
A label placed in Footer Cell of a RadGridView doesn't have sufficient width available in its container for displaying its full text.
Is it possible by any ways to have the label cross its container boundaries and show full text?
Thanks!
The space given to any element depends on the parent control or Panel that contains it, as well as its Width, Height, Horizontal and VerticalAlignment, and Margin. Normally you can manipulate some combination of these directly to change overlapping behavior but by using a DataGrid control you've given up a lot of that control since things like ColumnSpan are set up by the control internally. You could try setting negative Margin values and changing the Panel.ZIndex but I doubt those will help.
The best solution I can recommend without more detail is to use TextWrapping or TextTrimming to avoid ugly clipping, maybe in combination with a ToolTip showing the full text.
You can overlay any WPF element by another anytime. Only place where it fails is the WebBrowser control .The WPF WebBrowser has not been improved a single bit from WinForms WebBrowser. It is still the same simple activex control. However, you can bypass even that with a tooltip control.
I believe you can either set ClipToBounds=False to allow it to expand outside its area, or set TextWrapping=Wrap to allow the text to wrap.
EDIT: Forgot ClipToBounds is only honored in the Canvas control, so wrap your label in a Canvas and set ClipToBounds=False and it should work.

WPF scrollviewer

Is there a posibility to scroll to a specific place in a ScrollViewer from your code behind?
So something like the Slider element you can change the value property...
You need the ScrollToHorizontalOffset and ScrollToVerticalOffset methods.
Annoyingly, there aren't corresponding (settable) properties, so you can't databind the scroll position, but these methods do at least let you set it from code.
In most cases if you're trying to show a specific control it's simpler to call BringIntoView on any FrameworkElement (Panel, Control, etc) contained in the ScrollViewer which will take care of all the size and offset calculations for you.

how can we change zindex of a silverlight control programmatically?

I have some controls added in a stackpanel programmatically. What i want to do is that i want one of the controls in this stackpanel to be placed over another control. Specifically, I want to place button over an image in this stack panel. I couldn't find zindex property in c# codebehind. Although it seems very simple problem but i am unable to find any clue to solve this problem. Anyone please......??
Try placing all your controls on Canvas. Then you can set Zindex with:
this.controlName.SetValue(Canvas.ZIndexProperty, 10d);
Only the Canvas panel supports a ZIndex property. Stackpanel doesn't because each item is placed one after the other in the panel so they shouldn't overlap each other. This can be a little annoying at times when you have animated transforms moving the items about because the previous assumption isn't actually true.
In general though if you need to place items in a visual stack the Stackpanel isn't the right place for it. Perhaps a Canvas or you could use a Grid where the oridinal position of a element determines its "zorder" in a cell.
From xaml:
<StackPanel Canvas.ZIndex="1">
</StackPanel>

Resources