简体   繁体   English

WPF窗口中可拖动和自动调整大小的用户控件

[英]Draggable and Auto-Resized UserControls in WPF Window

I'm making a WPF Hud, and I'm not such a WPF expert. 我正在制作WPF Hud,但我不是这样的WPF专家。

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. 我可以通过使用Canvas容器并使用Window的MouseMove事件来实现第一个功能。

I can achieve the second feature by using a Grid container and setting childrens both properties HorizontalAlignment VerticalAlignment to Strech. 我可以通过使用Grid容器并将两个子项的Horizo​​ntalAlignment VerticalAlignment设置为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. 这将创建一个初始尺寸为600x400的Canvas对象。 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. 您还需要适当调整包含窗口的大小...常规可调整大小的窗口在垂直方向上具有39像素的铬色,在水平方向上具有16像素的铬,因此请考虑在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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