如何控制网格高度



我有一个包含2个子控件的网格。我有一个简单的堆栈面板和一个ListBox,它将驻留在网格中:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="5" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListBox Name="lstGroups" Grid.Row="0" />
        <StackPanel Grid.Row="2" />
<Grid>

问题是,我的ListBox渲染超过了分配给网格的可视屏幕区域。我如何确保我的ListBox占据了可用空间,但它不会渲染到第二行,在第二行我需要一个垂直滚动条来查看所有内容?

您可能应该为此使用DockPanel。此外,您还可以通过编程设置Listbox的高度,但这不是一种非常干净的方法

<Window x:Class="MobilWPF.Windows.testWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="testWindow" Height="300" Width="300">
    <DockPanel>
        <StackPanel DockPanel.Dock="Bottom" >
            <TextBlock Text="blah"/>
        </StackPanel>
        <ListBox Name="lstGroups"  />
    </DockPanel>
</Window>

namespace MobilWPF.Windows
{
    /// <summary>
    /// Interaction logic for testWindow.xaml
    /// </summary>
    public partial class testWindow : Window
    {
        public testWindow()
        {
            InitializeComponent();
            for (int i = 0; i < 200; i++)
            {
                lstGroups.Items.Add(i.ToString()); 
            }
        }
    }
}

最新更新