在 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 Designer 而言,您的技术就是有效的。这就是为什么我不使用 Visual Studio Designer。 ;)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)