WPF:将ListBox的高度限制为网格行的高度

时间:2016-09-05 11:20:57

标签: c# wpf xaml listbox grid

这是我的XAML的一部分:

<Grid HorizontalAlignment="Stretch"
      Margin="10"
      MaxHeight="{Binding Path=ActualHeight,
          RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
  <Grid.RowDefinitions>
    <RowDefinition Height="1*"/>
    <RowDefinition Height="Auto"/>
  </Grid.RowDefinitions>

  <TabControl Grid.Row="0" MinWidth="270" HorizontalAlignment="Stretch">
    <TabItem Header="CPU">
      <StackPanel Orientation="Vertical" >
        <ListBox Name="CPUListBox"
                 ItemsSource="{Binding CPUCounters, Mode=OneWay}"
                 SelectionMode="Multiple"
                 MaxHeight="{Binding RelativeSource={RelativeSource
                     AncestorType={x:Type Grid}}, Path=ActualHeight}"
                 BorderThickness="1"
                 BorderBrush="#FF8B8B8B"
                 SelectionChanged="CPUListBox_SelectionChanged"
                 ScrollViewer.VerticalScrollBarVisibility="Visible">
          <ListBox.ItemTemplate>
            <DataTemplate>
              <TextBlock>
                <Run Text="{Binding CounterName, Mode=OneWay}"/>
                <Run Text="{Binding InstanceName, Mode=OneWay}"/>
              </TextBlock>
            </DataTemplate>
          </ListBox.ItemTemplate>                                
        </ListBox>

        <Grid Name="CPUSelectButtonsGrid">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>

          <Button Grid.Column="0" Name="CPUSelectAllButton"
                  Margin="0,10,0,0"
                  Click="CPUSelectAllButton_Click">
            <TextBlock Text="SELECT ALL"/>
          </Button>
          <Button Grid.Column="1" Name="CPUUnSelectAllButton"
                  Margin="0,10,0,0"
                  Click="CPUUnSelectAllButton_Click">
            <TextBlock Text="UNSELECT ALL"/>
          </Button>
        </Grid>
      </StackPanel>
    </TabItem>

    <TabItem Header="Memory">
      <StackPanel Orientation="Vertical">
        <ListBox Name="RAMListBox"
                 ItemsSource="{Binding RAMCounters, Mode=OneWay}"
                 SelectionMode="Multiple"
                 BorderThickness="1"
                 BorderBrush="#FF8B8B8B"
                 SelectionChanged="RAMListBox_SelectionChanged">
          <ListBox.ItemTemplate>
            <DataTemplate>
              <TextBlock>
                <Run Text="{Binding CounterName, Mode=OneWay}" />
                <Run Text="{Binding InstanceName, Mode=OneWay}" />
              </TextBlock>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>

        <Grid Name="RAMSelectButtonsGrid">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>

          <Button Grid.Column="0" Name="RAMSelectAllButton"
                  Margin="0,10,0,0"
                  Click="RAMSelectAllButton_Click" >
            <TextBlock Text="SELECT ALL"/>
          </Button>
          <Button Grid.Column="1" Name="RAMUnSelectAllButton"
                  Margin="0,10,0,0"
                  Click="RAMUnSelectAllButton_Click" >
            <TextBlock Text="UNSELECT ALL"/>
          </Button>
        </Grid>
      </StackPanel>
    </TabItem>
  </TabControl>

  <StackPanel Grid.Row="1"
              Orientation="Horizontal"
              HorizontalAlignment="Right"
              Margin="0,15,6,0">
    <TextBlock Name="NumberOfSelectionsTextBlock" Text ="0"/>
    <TextBlock Text=" items selected"/>
  </StackPanel>
</Grid>

下面&#39;错误的是什么: 我在ListBox的每个标签中都有一个TabControl。当ListBox中的元素数量很大时,ListBox会覆盖按钮,而我无法访问这些按钮。我尝试将ListBox Height限制为MaxHeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ActualHeight}",但这太过分了 - 需要Height整个Grid,还涵盖了他们下面的ButtonsTextBoxes。我希望使用Window动态调整整个内容的大小,以免被切断。

我找不到合适的解决方法。我该怎么办?

1 个答案:

答案 0 :(得分:0)

这里的问题是你的堆栈面板在没有项目增长时会增长。更好的布局是使用WPF Grid,其中您可以有两行。 第二行将包含按钮,然后该行可以给定固定大小,因为第一行可以设置为Width = *

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="30" />
    </Grid.RowDefinitions>
</Grid>