【发布时间】:2019-12-22 08:17:56
【问题描述】:
我是使用 WPF 的初学者并尝试创建响应式应用程序。我阅读了许多关于 WPF 中响应式设计可能性的博客和网站。现在我尝试创建一个示例表单。请查看下图以在我的表单中查找元素结构。
在这张图片中,第一个红框布局是最大化窗口,第二个是调整大小或小屏幕布局
红框是主容器网格,必须列(列定义)
蓝框是主网格的两个孩子(第一个蓝框是网格,第二个是包装面板)
第二个蓝色框旁边的绿色框是环绕面板的子元素。
我尝试在调整窗口大小时更改包装面板内容,如上图。我的意思是 wrappanel 方向是水平的,如果右侧没有可用的空间,则子内容以换行符排列。
请看下面的示例代码。
<Window x:Class="ResponsiveWPFApp.Responsive"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ResponsiveWPFApp"
mc:Ignorable="d"
Title="Responsive" Height="450" Width="800">
<Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200*"/>
<ColumnDefinition Width="400*"/>
</Grid.ColumnDefinitions>
<Grid Background="Yellow">
</Grid>
<WrapPanel x:Name="wrPanel" Background="Aqua" Grid.Column="1" Orientation="Horizontal" ItemWidth="Auto">
<WrapPanel.Children>
<Grid x:Name="gd1" Height="400" Width="Auto" HorizontalAlignment="Stretch" Background="Black" >
<TextBlock>terdf</TextBlock>
</Grid>
<Grid x:Name="gd2" Height="400" Width="Auto" Background="Green" >
<TextBlock >sdfdf</TextBlock>
</Grid>
</WrapPanel.Children>
</WrapPanel>
</Grid>
</Grid>
</Window>
在我的代码换行面板中包含两个子元素,它没有填满换行面板的可用空间。
【问题讨论】:
-
<WrapPanel.Children>标签可以省略。 Panels 的嵌套元素被添加到 Panel.Children 集合中,因为它是 ContentPropertyAtrribute 的配置方式。你没有写 Grid.Children,是吗?
标签: c# .net wpf responsive-design