Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 自动调整WPF控件的大小_.net_Wpf - Fatal编程技术网

.net 自动调整WPF控件的大小

.net 自动调整WPF控件的大小,.net,wpf,.net,Wpf,我创建了以下WPF控件一个点和一个描述标签:*North Star <UserControl x:Class="StopPoint.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://sc

我创建了以下WPF控件一个点和一个描述标签:
*North Star

<UserControl x:Class="StopPoint.UserControl1"
         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" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Canvas>
        <Path Fill="SkyBlue" Stroke="Black" StrokeThickness="2">
            <Path.Data>
                <EllipseGeometry Center="10, 10" RadiusX="4" RadiusY="4"/>
            </Path.Data>
        </Path>
        <TextBlock Text="North Star" Canvas.Left="20" Canvas.Top="20"/>
    </Canvas>
</UserControl>

我会在小组里有很多明星。我是否可以自动调整控件的大小,使其尽可能小


什么东西?

把它包装在一个简单的堆栈面板中怎么样

<UserControl x:Class="StopPoint.UserControl1"
         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" 
         d:DesignHeight="300" d:DesignWidth="300">

    <StackPanel Orientation="Horizontal">
        <Ellipse Margin="4" Stroke="Black" Fill="Yellow" Height="10" Width="10"/>
        <TextBlock Text="North Star">
            <TextBlock.RenderTransform>
                <RotateTransform Angle="45"/>
            </TextBlock.RenderTransform>
         </TextBlock>
    </StackPanel>
</UserControl>

d:DesignHeight/Width属性仅在设计师中受尊重,即Blend或Visual Studio(苹果酒)设计师。您可以安全地删除它们,然后在设计器中看到您的控件,而不受任何约束


您描述的自动调整大小行为是许多容器控件的默认行为。尽量不要像Holstebroe建议的那样使用画布。还可以尝试查看变换-您可以应用旋转和平移变换来实现所描述的效果。

您需要在用户控件中创建自动调整大小的行为

WPF方法是让家长询问子元素他们需要多少空间(测量阶段),然后根据可用空间告诉他们实际得到多少空间(安排阶段)

在这里,您希望父对象的大小由子对象所需的空间决定。因此将顶级父级(画布/用户控件)的大小绑定到内容所需的空间-使用IMultiValueConverter获取求和的多个输入。请参阅我发布的关于创建usercontrol以在形状中获取居中文本的链接,这是上述示例。
一旦你有了这个,你也可以使用一个视图框来做一个“适合屏幕”

        <!-- 36 <= 20  (canvas.top) + 16 (textblock.actualheight) -->
        <!--75 <= 20 (canvas.left) + 55 (textblock.actualwidth)-->
        <Canvas Background="SkyBlue" Height="36" Width="75">
        <TextBlock Canvas.Left="25" Text="{Binding ElementName=label, Path=ActualHeight}"/>
        <Path x:Name="dot" Fill="SkyBlue" Stroke="Black" StrokeThickness="2">
                <Path.Data>
                    <EllipseGeometry Center="10, 10" RadiusX="4" RadiusY="4"/>
                </Path.Data>
            </Path>

        <TextBlock x:Name="label" Text="North Star" Canvas.Left="20" Canvas.Top="20"/>


首先,问题是标签的位置需要定制。(文本位置的半径和角度),之后用户控件d:DesignHeight=“300”d:DesignWidth=“300”也不好…DesignHeight只是为了方便设计者使用。一旦你开始在另一个控件中使用它,这并不重要。我已经在文本块中添加了一个RotateTransform。您也可以添加任何其他变换或文字效果来满足您的需要。画布应该是您的最后一个选项。如果您想使用画布,您可以将其包装在Viewbox中以控制其大小,但这可能无法获得您想要的自动大小。这很酷。。。但当旋转时,铭文会越过椭圆…:)使用RotateTransform的CenterX和CenterY属性将轴心点放置在星形的中心。您还可以将RotationTransform与其他变换组合。