Displaying Both VisualCollection and UIElements - c#

I'm using a VisualCollection to display custom DrawingVisuals on a Canvas in WPF, and I also need the canvas to display a couple of UIElements. The problem is that the canvas seems to ignore its regular children when using a VisualCollection. The UIElements don't appear when placed in the VC either.
I want the UIElements to be rendered above the DrawingVisuals, so I cannot (?) place the DVs in an AdornerLayer. Any ideas?

Okay, so I sort of found a solution.
I simply create a new FrameworkElement that hosts all the DrawingVisuals through a VisualCollection, and add this host to the canvas as a regular child.

Related

Put an adorner layer behind child controls

I have a canvas where I need to draw a grid, but behind child controls. I tried to create a new adroner and just add it to the canvas, but this adorner is displayed over child controls. How can I make this adorner be behind canvas' children? I don't want to draw this grid in the OnRender function of the canvas...
Clarification: grid is not a control. it is a painted grid or net on the canvas. it is created for making position elements easier.
One way to do this is to use a TileBrush as the Canvas.Background and set the TileBrush to an appropriate grid pattern.
An Adorner can only ever be on top (it allways has the highest zindex). You need to look at another solution to your underlying issue
Use the Panel.ZIndex Property to set the order elements get displayed on the z-plane.

WPF: How to find space available for Canvas?

I'm using WPF shapes to create Hexagons (for a game map) on a Canvas. After some playing around with ScrollViewer, I've decided to implement the scrolling and zoom of the map myself rather than using WPF functionality, just using WPF to get the events for mouse wheel, arrow keys etc. I'm placing the (Hex Map) Canvas as the last child inside a Dock Panel so it will get all the available remaining space. The Dock Panel will be set to be the content of the Main Window. But I want to find out how big the Canvas can be before I put any Children on the Canvas so that I can centre the screen over the Hex I want and only add the Shapes (Hexs) that can actually be seen. When zoomed out, a long way I will remove Polygons altogether and use another method of rendering and when zoomed in a long way I will add more details.
Is there any neat way of getting the available space? The only way that I can think of that will hopefully work is to get the current dimensions of the windows and subtract the dimensions of the outer elements of the Dock Panel, but that feels rather messy.
You may use the ActualWidth and ActualHeight properties of Canvas to determine size available to it. Be sure that HorizontalAlignment and VerticalAlignment are set to Stretch.

Custom Items in Lists in C# forms?

I'm a bit new to the C#-form app developing and I want know, what's the best way around at making a control that holds a list of horizontal items. In which each of these items are horizontally ruled to it's parent control, contain a thumbnail to the left and a large text block to the right of image and a smaller text block underneath that. So basically this isn't a predefined control I can find in the toolbox. Any ideas?
You could lay this out with Panels in form controls, or with WrapPanel and StackPanel in WPF.
In WindowsForms, I would create a user control that held the correct layout for a single item, then make a list of them at run time.
In WPF I would use a List control, but set the layout template to use WrapPanels and StackPanels.
WPf is the better solution long term if you don't have to coexist with winforms

How to reuse WPF ScrollViewer to create my own scrollable control?

I'm trying to improve the graph drawing control that comes with Graph#. It's good, but things get out of hand when you start dragging nodes around. This is my first encounter with WPF, so this is probably a newbie question. :)
I have the GraphCanvas control which has nodes and edges on it. They can be dragged around which changes their coordinates, possibly making them negative. I would like to add scrollbars to the control which would allow to see how big the canvas really is.
To this end I'm thinking of putting the GraphCanvas inside a ScrollViewer. Which would be pretty easy and straightforward if not for one problem. I may not resize the GraphCanvas itself when a node is dragged outside the borders or this will mess up dragging bad. That is also the problem with the original control (check it out, it comes with a sample application).
It would be good if I could bind the scrollbar size/location to properties of the GraphCanvas, so that the ScrollViewer would not scroll anything physically, but just set the properties of GraphCanvas. That in turn would perform all actual calculations and scrolling.
How can this be done?
OK, I found it! Three easy steps:
Implement System.Windows.Controls.Primitives.IScrollInfo on your custom control;
Add your custom control to a ScrollViewer;
Set the CanContentScroll property on the ScrollViewer to True.
Voila!
Check out this link straight from MSDN. It talks about composing several controls into a single Composite Control:
WPF: Customizing Controls for Windows Presentation Foundation

Resize ElementHost to size of the hosted XAML UserControl

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.

Categories