简体   繁体   中英

Draggable and Auto-Resized UserControls in WPF Window

I'm making a WPF Hud, and I'm not such a WPF expert.

I need a Window with draggable usercontrols and which are auto-resized when I expand/reduce the Window.

I can achieve the first feature by using a Canvas container and using MouseMove event of the Window.

I can achieve the second feature by using a Grid container and setting childrens both properties HorizontalAlignment VerticalAlignment to Strech.

Of course I cannot use both containers, so is there a third container that can help me on doing such what requested?

Actually I'm trying to use the Canvas and determining the width/height of the childrens manually, but I don't like this approach.

Any suggestion?

Thanks, Luca

Leaving out the details on dragging controls, etc., try something like:

<Window x:Class="AdHocWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="439" Width="616"
    Title="MainWindow">
    <Viewbox>
        <Canvas Width="600" Height="400" Background="Blue">
            <Rectangle Stroke="Orange" Width="200" Height="100" Canvas.Left="10" Canvas.Top="5"/>
            <Ellipse Stroke="Yellow" Width="350" Height="200" Canvas.Left="225" Canvas.Top="150"/>
        </Canvas>
    </Viewbox>
</Window>

This will create a Canvas object with an initial size of 600x400. When you resize the window the canvas and the objects within it will be scaled so that they take up the same amount of relative space. You'll need to size the containing Window appropriately as well...normal resizable windows have 39 pixels of chrome vertically and 16 pixels horizontally, so take that into account.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM