我试图在一个UWP应用程序中构建一个UserControl,但是我在XAML-Designer中看不到它的“自动”大小(基于它的内容)。
我知道我可以将d:DesignHeight和d:DesignWidth硬编码为绝对值,但是因为大小应该基于其内容,所以我不想硬编码这些值。
有没有像d:DesignHeight=“自动”/d:DesignWidth=“自动”这样的东西?
例如-对于下面的示例用户控件
<UserControl
x:Class="MyCompany.UserControls.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:MyCompany.UserControls.UserControls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel>
<Button Content="First" />
<Button Content="Second" />
</StackPanel>
</UserControl>我看到了

但是,我希望看到基于其内容的控件的大小。
发布于 2020-06-29 16:58:46
不幸的是,这是XAML设计器的一个限制。该控件在运行时仍将适当调整大小。
然而,这是一个非常好的反馈,我建议您通过Visual Studio反馈将其作为增强请求发送,我肯定会支持它:-)。
发布于 2020-06-30 10:49:37
如果在用户控件中删除d:DesignHeight & d:DesignWidth,它将根据其内容进行布局。
如果没有,那么您可以在使用时将VerticalAlignment & HorizontalAlignment添加到您的用户控件。
<local:MyUserControl2 VerticalAlignment="Top" HorizontalAlignment="Left"
BorderBrush="Red" BorderThickness="1"/>应该能行得通。
https://stackoverflow.com/questions/62633953
复制相似问题