C# 用户控件在运行时调整大小

C# 用户控件在运行时调整大小,c#,wpf,user-controls,C#,Wpf,User Controls,各位程序员 我正在开发一个WPF软件,它使用画布来显示和移动图形对象。 这些图形对象是包含标签或矩形的用户控件: <UserControl x:Class="DashEditor.Views.MovableObject" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xam

各位程序员

我正在开发一个WPF软件,它使用画布来显示和移动图形对象。 这些图形对象是包含标签或矩形的用户控件:

<UserControl x:Class="DashEditor.Views.MovableObject"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" >


<Grid Name="ControlLayout">

    <StackPanel x:Name="DisplayPanel" >

        <Canvas x:Name="graphicObjectCanvas" Width="100" Height="50">

            <Viewbox x:Name="graphicObjectViewBox" Width="100"  Height="50" IsEnabled="False" Canvas.Left="0" Canvas.Top="0" Stretch="Fill"/>

        </Canvas>

    </StackPanel>

</Grid>

我需要调整这些用户控件的大小,我看到了一些例子,但我不知道如何在用户控件中使用它


谢谢你的帮助

好的,我找到了一个有效的解决方案来调整画布图形对象的大小。 我在usercontrol中添加了一个拇指,当选择图形对象时,该拇指将可见:

<Grid Name="ControlLayout">

    <StackPanel x:Name="DisplayPanel">
        <Border x:Name="CanvasBorder" BorderThickness="1">
            <Canvas x:Name="graphicObjectCanvas" Width="100" Height="50" Background="Aquamarine">
                <Viewbox x:Name="graphicObjectViewBox" Width="100"  Height="50" IsEnabled="False" Stretch="Fill"/>

                <Thumb x:Name="myThumb" Canvas.Left="80" Canvas.Top="30" Width="20" Height="20" DragDelta="myThumb_DragDelta" Visibility="Hidden"
                       PreviewMouseLeftButtonDown="myThumb_PreviewMouseLeftButtonDown" PreviewMouseLeftButtonUp="myThumb_PreviewMouseLeftButtonUp" BorderBrush="Blue" BorderThickness="2"/>

            </Canvas>
        </Border>

    </StackPanel>

</Grid>
    private void myThumb_DragDelta(object sender, DragDeltaEventArgs e)
    {
        double yadjust = graphicObjectViewBox.Height + e.VerticalChange;
        double xadjust = graphicObjectViewBox.Width + e.HorizontalChange;
        if ((xadjust >= 0) && (yadjust >= 0))
        {
            graphicObjectViewBox.Width = xadjust;
            graphicObjectViewBox.Height = yadjust;
            graphicObjectCanvas.Width = xadjust;
            graphicObjectCanvas.Height = yadjust;
            Width = (int)xadjust;
            Height = (int)yadjust;
            XapParent.Width = (int)xadjust;
            XapParent.Height = (int)yadjust;
            Canvas.SetLeft(myThumb, Canvas.GetLeft(myThumb) + e.HorizontalChange);
            Canvas.SetTop(myThumb, Canvas.GetTop(myThumb) + e.VerticalChange);
        }
    }