WPF ,使垂直拉伸按预期工作! [英] WPF , Getting verticalstretch working as expected!

查看:50
本文介绍了WPF ,使垂直拉伸按预期工作!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使垂直拉伸按预期工作,我 WPF,但由于某种原因,它只占用了它需要的空间,而不是可用空间.

I am trying to make the vertical stretch to work as expected i WPF, but for some reason it only takes the space it needs, and not the space available.

首先,我将 WPF 与 C# 和 Prism 结合使用.

First, I am using WPF with C#, and Prism.

在 Shell.xaml(应用程序的主 xaml)中,我有一个包含 2 列和 1 行的网格.这个想法是有一个侧面板和一个主应用程序区域.主应用程序区域网格设置为自动宽度和自动高度.这是按预期工作的,它确实可以缩放以适应整个应用程序的高度和宽度.

In the Shell.xaml (main xaml for the application) I have a grid with 2 columns and one row. The idea is to have a side panel and a main app area. The main app area grid is set to Auto Width and Auto Height. This is working as expected, and it does scale to fit the whole application in Height and Width.

然后我使用棱镜将视图(作为 UserControl 组件)插入到主应用程序区域.UserControl 也设置为 Auto Width 和 Height,通过查看 Expression Blend 中的默认设置,Horizo​​ntalAlignment 和 VerticalAlignment 设置为拉伸!

Then I am using prism to insert the view (as a UserControl component) into the main app area. The UserControl is also set to Auto Width and Height, and by looking at the default settings in Expression Blend, the HorizontalAlignment and the VerticalAlignment are set to stretch!

然而,加载 UserControl 的棱镜只在宽度上拉伸而不是在高度上拉伸!通过给它一个视觉反馈的背景颜色,我可以看到它只根据需要占用垂直空间,而不是整个可用区域!

However, the prism loaded UserControl only stretches in Width and not in height! By giving this a background color for visual feedback, I can see that it only takes the vertical space as needed, and not the whole area available!

有什么办法可以解决这个问题?我已经尝试通过所有设置手动将它们覆盖为宽度和高度自动、水平和垂直对齐到拉伸,但似乎没有任何效果!

What could be the solution for this? I have tried going trough all settings an manually overriding them to Width and Height Auto, Horizontal and Vertical Alignment to Stretch, but nothing seems to work as expected!

一些代码:(Shell.xaml)

Some code: (Shell.xaml)

<Window Height="1140" Width="1450">
    <Grid Margin="0" Background="White">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="250" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid Background="#FFEEEEEE" Grid.Column="0" Grid.Row="0">   
            </Grid>          
            <ItemsControl Width="Auto" Height="Auto" Grid.Row="0" Grid.Column="1" Name="MainRegion" cal:RegionManager.RegionName="MainRegion" Padding="10" Margin="10, 0, 0, 0" Background="#FFFFFFD5" />
        </Grid>
    </Grid>
</Window>

(应该继承父高度的视图):

(view that should inherit the parent height):

<UserControl Width="Auto" Height="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
     <!-- I want this to take the full width and height of the parent -->
</UserControl>

这是将视图加载到主 xaml 的方式的限制,还是 UserControl 的限制,或者我不明白的其他内容?

So is this a limitaion in the way the views are loaded into the main xaml, or is this a UserControl limitation, or something else that I do not understand?

只是为了澄清;我确实看到了 Shell.xaml 中定义的 ItemsControl 的背景颜色同时拉伸了水平和垂直,但没有看到加载到 ItemsControl 中的视图的背景.

Just to clarify; I do see the background color of ItemsControl defined in Shell.xaml stretching both Horizontal and Vertical, but not the background of the view loaded into ItemsControl.

请注意,我删除了一些 xaml 以便更容易理解!

Note that I have removed some of the xaml to make the point easier to understand!

谢谢!

推荐答案

我找到了解决方案.我必须覆盖 ItemsPanel 模板才能使用网格(或类似的容器):

I found a solution. I have to override the ItemsPanel Template to use a grid (or similar container):

<ItemsControl>
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <Grid/>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

在这种情况下,我还删除了一些 xaml 以使其更具可读性!

I have also in this case removed some of the xaml to make it more readble!

谢谢

这篇关于WPF ,使垂直拉伸按预期工作!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆