gpt4 book ai didi

WPF UserControl 设计时间大小

转载 作者:行者123 更新时间:2023-12-01 21:57:05 26 4
gpt4 key购买 nike

在 WPF 中创建 UserControl 时,我发现给它一些任意的高度和宽度值很方便,以便我可以在 Visual Studio 设计器中查看我的更改。但是,当我运行该控件时,我希望高度和宽度未定义,以便控件将展开以填充我放置它的任何容器。如何实现相同的功能,而不必先删除高度和宽度值建立我的控制权? (或者不在父容器中使用 DockPanel。)

以下代码演示了该问题:

<Window x:Class="ExampleApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:ExampleApplication3"
Title="Example" Height="600" Width="600">
<Grid Background="LightGray">
<loc:UserControl1 />
</Grid>
</Window>

UserControl1 的以下定义在设计时合理显示,但在运行时显示为固定大小:

<UserControl x:Class="ExampleApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid Background="LightCyan" />
</UserControl>

以下 UserControl1 定义在设计时显示为点,但在运行时扩展以填充父 Window1:

<UserControl x:Class="ExampleApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Background="LightCyan" />
</UserControl>

最佳答案

对于 Blend,一个鲜为人知的技巧是将这些属性添加到您的用户控件或窗口:

 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="600"

这会将设计高度和宽度分别设置为 500 和 600。然而,这仅适用于混合设计师。不是 Visual Studio 设计器。

就 Visual Studio 设计器而言,您的技术就是有效的。这就是为什么我不使用 Visual Studio Designer。 ;)

关于WPF UserControl 设计时间大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com