【问题标题】:Visual Studio "Auto" width / height in XAML-DesignerXAML-Designer 中的 Visual Studio“自动”宽度/高度
【发布时间】:2020-10-19 08:52:48
【问题描述】:

我正在尝试在 UWP 应用程序中构建用户控件,但我无法在 XAML 设计器中看到其“自动”大小(基于其内容)。

我知道我可以将 d:DesignHeight 和 d:DesignWidth 硬编码为绝对值,但由于大小应基于其内容,我不想硬编码这些值。

有没有类似 d:DesignHeight="Auto" / d:DesignWidth="Auto" 的东西?

例如- 对于下面的示例用户控件

<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>

我看到了

但是我想根据内容查看控件的大小。

【问题讨论】:

    标签: visual-studio xaml uwp


    【解决方案1】:

    很遗憾,这是 XAML 设计器的限制。控件在运行时仍会正确调整大小。

    但是,这是一个非常好的反馈,我建议您通过 Visual Studio 反馈将其作为增强请求发送,我肯定会赞成 :-)。

    【讨论】:

    【解决方案2】:

    如果在用户控件中删除d:DesignHeight d:DesignWidth,它将根据其内容进行布局。

    如果没有,那么您可以在使用时将VerticalAlignmentHorizontalAlignment 添加到您的用户控件中。

    <local:MyUserControl2  VerticalAlignment="Top" HorizontalAlignment="Left" 
        BorderBrush="Red" BorderThickness="1"/>
    

    它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2010-12-11
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      相关资源
      最近更新 更多