【问题标题】:How to get the Usercontrol to keep its size once put on the Page?一旦放在页面上,如何让用户控件保持其大小?
【发布时间】:2019-10-25 04:20:00
【问题描述】:

我正在创建一个 UWP 应用。在应用程序中,我创建了一个用户控件,其中有一个设置大小的矩形。尺寸为 700x700。我还将用户控件设置为 700x700。

我认为它会起作用的是,当开发人员将用户控件放在页面上时,用户控件将出现,大小为 700x700。

但是,用户控件的显示尺寸为 100x100!

这是我在工具栏中双击用户控件以将其添加到页面后的页面代码。

<Custom:Control HorizontalAlignment="Left" Height="100" Margin="254,158,0,0" VerticalAlignment="Top" Width="100"/>

注意高度和宽度是 100x100!

我在 XAML 和类构造函数中将 Usercontrol 的 Height 和 Width 属性设置为 700x700。什么都没有改变!

我已经为 Usercontrol 中的所有子控件设置了 Horizo​​ntalAlignment 和 VerticalAlignment 属性。什么都没有改变!

这是用户控件 XAML

<Usercontrol
    x:Class="Circles.CustomControls.Control"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Circles.CustomControls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="700" Height="700">

    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Canvas x:Name="Container" Background="Transparent" Width="700" Height="700" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Rectangle x:Name="Card" Stroke="Black" Width="700" Height="700" Fill="White" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        </Canvas>
    </Grid>
</UserControl>

这是用户控件的代码隐藏

public sealed partial class Control : Usercontrol
{
   public Control()
   {
      this.InitializeComponent();
      this.Height = 700;
      this.Width = 700;
   }

}

我希望当开发人员将用户控件放到页面上时,会出现一个大小为 700x700 的用户控件。

更新 1 这是一些图片

Image1 - This shows the issue with the bounding rectangle

Image2 - This show the DatePicker control

用户控件和日期选择器控件都以相同的方式添加到页面。

  • 从工具箱中选择控件并将其拖到页面上。

Image1 是用户控件

  1. 较小的矩形 100x100
  2. 用户控件和画布的控件大小均设置为 700x700
  3. 画布尺寸显示为 700x700
  4. VS 属性窗口显示 100x100
  5. VS 页面 XAML 将高度和宽度显示为 100x100

Image2 是日期选择器

  1. 边界矩形遵循内部控件的可见轮廓
  2. VS 属性窗口设置了默认大小

@Nico Zhu - MSFT 和@Faenrig - 我希望这能说明我的问题。如果你所做的事情是有效的,那么我很想知道我做错了什么或者没有做什么????

【问题讨论】:

  • 为什么Control构造函数中没有InitializeComponent();
  • 抱歉,我的复制粘贴编辑错误
  • 我检查了您的屏幕,为什么您将TargetAirRiFle2 宽度设置为100 在网格中的哪个位置?

标签: c# uwp uwp-xaml


【解决方案1】:

如何让用户控件在页面上保持其大小?

Usercontrol 的大小将受其父容器的约束。如果父级大小限制为 100*100,您的 Usercontrol 将被调整大小。所以请编辑父容器。

<StackPanel Orientation="Vertical" Height="700" Width="700">
    <local:CustomLabel/>
</StackPanel>

【讨论】:

  • 嗨。我试过了,但它没有解决他们“如何让用户控件在页面上保持其大小?”题。当您从工具箱中双击或拖动用户控件时,它会像这样&lt;Custom:Control HorizontalAlignment="Left" Height="100" Margin="254,158,0,0" VerticalAlignment="Top" Width="100"/&gt; 放置在页面中
  • 所以我需要解决的是如何从预先填充的 Height 和 Width 属性中获取它。
  • 我尝试从工具箱中拖动自定义控件,但无法重现此问题,请提供可以重现此问题的迷你代码示例。
  • 我也尝试过重现该问题。我根本做不到,所以一个例子会有所帮助。就我而言,我尝试了几种大小的 MainWindow 及其上的控件。最有趣的是 MainWindow 和 700x700 的控件,但控件位于 100x100 大小的 Stackpanel 中。您可以清楚地看到控件没有完全由边框顶部和左侧显示。仍然正确调整了大小值,所以这个问题现在太广泛了。
  • @NicoZhu-MSFT - 我发布了一些我看到的图片。它还显示了代码。
猜你喜欢
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-26
相关资源
最近更新 更多