Resize ElementHost to size of the hosted XAML UserControl - c#

I would like an expanding panel in my Windows Forms app. I was having a look to see if this would be possible using the WPF Expander control. I've created a Xaml UserControl where I've inherited from Expander rather than UserControl. I have a trigger on the Expander for setting it's size.
Is it possible to change the height of the ElementHost to reflect the change in the size of the child? Or would I just be better off creating an expanding Panel in Windows Forms?
I'm using C# .Net 3.5.
Cheers

Yes. You need to override MeasureOverride in your outermost WPF control, convert the size from WPF coordinates to device coordinates, and update ElementHost.Size.
Since you are already subclassing Expander:
Override the MeasureOverride method
After the measurement is calculated, use PresentationSource.From(visual).CompositionTarget.TransformToDevice.Transform(point) to get the device coordinates
Update ElementHost.Size.
Your Expander subclass instance will need a pointer to ElementHost to do this.
A more general solution would be to create a new class to handle the synchronization. It would subclass FrameworkElement and be the direct child of ElementHost.

Is there any particular reason you are inheriting from Expander vs. just using an Expander in your UI?
If you set up the H/V Alignment properties of the Expander, you should be able to get most standard sizing behaviors without having size triggers. From my experience, the content portion of the Expander automatically sizes to fit.
If you're trying to completely remove the header part, then you might look at making your own ControlTemplate for the Expander.

Related

WPF - Docking content inside ContentPresenter

The Circumstances
I'm building a plugin for a WPF application. The parent application has a ContentPresenter and my plugin returns a UserControl. When the parent app runs, it loads up my plugin and places my UserControl inside the ContentPresenter.
The Problem
The problem is that my UserControl is not resized to fit within the ContentPresenter, so when the ContentPresenter is too small to show everything, my UserControl is simply cut off. If the app is resized so that the ContentPresenter becomes bigger than my UserControl, then I get a bunch of whitespace around my UserControl.
Basically, I want my UserControl to act as if it was inside a DockPanel.
What I've Tried
Both the ContentPresenter and the UserControl have Horizontal and Vertical alignments set to Stretch already.
I can achieve the desired result if I wrap my UserControl in a ViewBox, but I don't really know if that's the right way to do it. It feels like I'm missing out on some basic layout principle (which might just be a holdover from my Winforms days). Is there any "proper" way to achieve a docking effect within a ContentPresenter element?
EDIT: I want to emphasize that this is not a ContentControl vs. ContentPresenter issue. I cannot change out the ContentPresenter element because it is part of the parent application. The only thing I can control is the contents of my own plugin which are being placed within the ContentPresenter element. The developers of the parent application might allow me LIMITED access to the ContentPresenter if I need to adjust a property by traversing up the visual tree at runtime, but they've already stated that they don't want plugins navigating outside of their own scope of controls. This isn't as much of a technical limitation as a their-terms-and-conditions-won't-allow-it limitation.

Resizing SplitContainer makes controls invisible

Why does the WinForms SplitContainer hide all the Buttons in a Panel (in the left Splitter) when I resize on of its panels? Do I have to write specific code for invalidation of the Panel?
Yes, there's a solution. You can use Anchor property of SplitContainer in winforms. Using which you can set the value of the anchor as you required.
EDIT:
Anchor property will make your control fixed to the winforms design. Even if you resize the control, all the controls that the major control is holding will also resize according to it.

Treeview node in vc# for Auto Ellipsis property

I have a tree view control which has some nodes. I want Auto Ellipsis property to tree view node as like label control have. I did Google but not get relevant information or method to achieve this task.
I am using c# as a programing language and .net framework 3.5 as a platform. I am making program in windows form not in WPF.
Thanks.
You're right, that property doesn't exist. The TreeView control will display a horizontal scrollbar if the labels for any of the nodes are longer than the viewable area for the control.
The only option you have to replace this with an ellipsis is to owner-draw the control. But since you only want to change the text of the nodes, you can set the DrawMode property to OwnerDrawText (rather than OwnerDrawAll), which significantly reduces the burden of owner drawing.
You can find sample code for owner drawing a TreeView control here on MSDN.
Remember that the ClientRectangle property of a control, including the TreeView control, will tell you the actual width of the control's client area. Use this to determine if the node label is too long, and whether or not it needs to be drawn with ellipses.
The various overloads of the TextRenderer.DrawText method will allow you to draw the node labels in a way that resembles the way the native control draws itself (i.e., using GDI rather than GDI+). Passing an appropriate combination of TextFormatFlags will instruct the method to automatically trim text and replace it with an ellipsis.

How do I make multiple controls in a Windows Form automatically resize with the window?

I'm new to Windows Forms in Visual Studio, and I am wondering how to automaticly resize controls to the window size.
Say, I have 2 controls in a panel, a List Box and a Button. I want the button to dock to the bottom, and I want the List Box to fit the rest of the space. when the window resizes, the button should be at the bottom (as expected with docking), and the list box should stretch down to the button.
Is there a way to do this without any code?
Thanks.
Dock is pretty easy to use, but I recommend using the Anchor properties instead. Resize your form to a reasonable size in the Designer. Then, place your controls to look the way you want. Then, decide which controls should resize with the form and set the Anchor property as follows:
If you want the control to resize with the form in width, set the Right anchor.
If you want to resize height, set the Bottom anchor.
If you want the control to stay right when the form resizes, unset the Left anchor.
If you want the control to stay bottom when the form resizes, unset the Top anchor.
The problem I have with Docks is that they sometimes act funny when controls are not declared in a specific order, and to get the effect you want, sometimes you have to create extraneous panels just to hold controls.
It really gets messy when you want to maintain the aspect ratio of each control. One way, which is not really up to the mark if you want to get into fixing the details, is to use TableLayoutPanel and use Dock and Anchor wisely to achieve what you want.
Use the dock and fill options on the controls. Look under properties for each object, and containers if they are in any.
You can use SplitContainer
Google for examples. Here is one
Try setting your ListBox's Dock property to Fill.
You'll need to watch for one thing though: by default the ListBox will size itself to display whole list items. If you resize the control so that it displays a partial item it will adjust itself so it will display a complete item. This can make the control appear to lose its 'Dock'ing behavior. The solution for this is to set the ListBox's IntegralHeight property to false, which specifies that the control not resize itself to fit complete items.

Resizing componenets on a c# form

typically in java if you have a layout manager of somesort, when you resize the page then the components in that panel will resize accordingly. I think my app is missing some sort of layout manager to control resizing as at the moment everything is just static
Is there a way to make it resize on the changing of the form size? say the user makes the page bigger, then the componenets adjust and so on.
Thanks
.NET has layout managers as well.
Personally, I prefer the TableLayoutPanel for my WinForms apps.
Once you layout the Table (using a combination of static/dynamic sized rows/columns) you add your child controls to the table cells. Once you add your controls, you can dock or anchor the controls to the cell so that they are automatically adjusted when the window is re-sized.
Two main options:
Anchoring. Set your control to "anchor" to the sides of your form. This means that if the form resizes, the control will stay a constant distance from that side. So, if you anchor Top, Left and Right, then your control will stay in the same position, but resize horizontally with the width of the form. Play with it. It'll be obvious.
Docking. Set your control to "dock" to a side of the form, or the center. This is usually done with containers, and it will make the widget take up that entire portion of the form no matter how large it gets.
In Windows Forms you make use of the Control.Anchor property, which will cause the control to adjust accordingly when the window resizes.
To do this with windows forms you use the Anchor and Dock properties of the control
See this for a guide on how to use them

Categories