首页 文章

如何让WPF ListBox始终是4个网格行的高度?

提问于
浏览
1

即使我在列表框中添加了几个项目,如何才能使我的列表框成为行的标准高度?我也不想在两者之间设置高度 . 我已经尝试设置一些共享大小组,尝试将每个行的高度绑定到不同的行等,没有解决方案 . 我希望列表框占用的四行高度与原来的高度相同 . 列表框当前增长以适应其中的所有项目,因此使行更高,这是我不想要的 . 如果列表框高于4“正常”行高,我希望列表框能够获得滚动条 . 如果我必须在列表框上设置maxHeight,我怎么能告诉它是我其他行之一的高度的4倍?

XAML:

<Window x:Class="SharedSizes.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Border Margin="10" >
    <StackPanel Grid.IsSharedSizeScope="True">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="B"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="C"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="D"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="E"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="F"/>

            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition  Name="Row1" Height="Auto" SharedSizeGroup="Width"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <!--Left Side Parts-->

            <Label Content="Vendor:" Grid.Row="0" Grid.Column="0"/>
            <ComboBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4" />

            <Label Content="Billing Model:" Grid.Row="1" Grid.Column="0" />
            <ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Width="125"/>

            <Label Content="Billing Cycle:" Grid.Row="2" Grid.Column="0"/>
            <ComboBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" />

            <StackPanel Grid.Row="2" Grid.Column="3" Orientation="Horizontal" HorizontalAlignment="Left">
                <Button Content="1"/>
                <Button Content="2"/>
                <Button Content="3"/>
                <Button  Content="4"/>
            </StackPanel>
            <Label Content="Clients" Grid.Row="2" Grid.Column="5"/>
        </Grid>
        <Grid>
            <Grid.RowDefinitions>
                <!--<RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}"/>
                <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" />
                <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" />
                <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" />-->
                <RowDefinition SharedSizeGroup="Width"/>
                <RowDefinition SharedSizeGroup="Width"/>
                <RowDefinition SharedSizeGroup="Width"/>
                <RowDefinition  SharedSizeGroup="Width"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="B"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="C"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="D"/>
                <ColumnDefinition Width="Auto"  />
                <ColumnDefinition  />
            </Grid.ColumnDefinitions>

            <Label Content="Fred" Grid.Row="0"/>
            <Label Content="Fred" Grid.Row="0" Grid.Column="1"/>
            <Label Content="Fred" Grid.Row="0" Grid.Column="2"/>
            <Label Content="Fred" Grid.Row="0" Grid.Column="3"/>
            <Label Content="Fred" Grid.Row="0" Grid.Column="4"/>
            <Label Content="Fred" Grid.Row="1"/>
            <Label Content="Fred" Grid.Row="2"/>
            <Label Content="Fred" Grid.Row="3"/>
            <ListBox Name="FredBox" Grid.Row="0" Grid.Column="5" Grid.RowSpan="4"  Grid.IsSharedSizeScope="True"/>
        </Grid>
    </StackPanel>

我希望它总是看起来(以及它如何处理列表框中没有项目):

enter image description here

将项目添加到列表框后它做了什么:

enter image description here

编辑VIV的建议:

<Window x:Class="SharedSizes.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <!--<Style TargetType="{x:Type RowDefinition}">
        <Setter Property="Height" Value="25"/>
    </Style>-->
</Window.Resources>

<Border Margin="10" >
    <StackPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition  />
                <ColumnDefinition  />
                <ColumnDefinition  />
                <ColumnDefinition  />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>

            <!--Left Side Parts-->

            <Label Content="Vendor:" Grid.Row="0" Grid.Column="0" />
            <ComboBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3" />

            <Label Content="Billing Model:" Grid.Row="1" Grid.Column="0" />
            <ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" />

            <Label Content="Billing Cycle:" Grid.Row="2" Grid.Column="0"/>
            <ComboBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" />

            <StackPanel Grid.Row="2" Grid.Column="3" Orientation="Horizontal" >
                <Button Content="1"/>
                <Button Content="2"/>
                <Button Content="3"/>
                <Button  Content="4"/>
            </StackPanel>
        </Grid>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}"/>
                <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/>
                <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/>
                <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition  />
                <ColumnDefinition  />
                <ColumnDefinition  />
                <ColumnDefinition  />
                <ColumnDefinition  />
                <ColumnDefinition  />
            </Grid.ColumnDefinitions>

            <Label Content="Fred" Grid.Row="0" x:Name="SomeLabel"/>
            <Label Content="Fred" Grid.Row="0" Grid.Column="1"/>
            <Label Content="Fred" Grid.Row="0" Grid.Column="2" />
            <Label Content="Fred" Grid.Row="0" Grid.Column="3"/>
            <Label Content="Fred" Grid.Row="0" Grid.Column="4"/>
            <Label Content="Fred" Grid.Row="1"/>
            <Label Content="Fred" Grid.Row="2"/>
            <Label Content="Fred" Grid.Row="3"/>
            <ListView Name="FredBox" Grid.Row="0" Grid.Column="5" Grid.RowSpan="4"  />
        </Grid>
    </StackPanel>
</Border>

Using Viv's suggestions

1 回答

  • 1

    那么你可以通过使用每一行中另一项(你拥有的 Label )的 DesiredSize.Height 设置 Grid.RowHeight 来做到这一点 .

    所以通过切换:

    ...
    <!--<RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}"/>
        <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" />
        <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" />
        <RowDefinition Height="{Binding ElementName=Row1, Path=ActualHeight}" />-->
    ...
    <Label Grid.Row="0"
           Content="Fred" />
    ...
    

    <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}" />
    <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}" />
    <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}" />
    <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}" />
    ...
    <Label x:Name="SomeLabel"
            Grid.Row="0"
            Content="Fred" />
    ...
    

    我只是将 RowDefinition.Height 的所有4个绑定到相同的 Label ,因为所有行都具有相同的项目内容和样式,但是你希望得到这张照片 . 如果它们是每行中的不同项,请为它们指定相应的名称,并将每个 RowDefinition 绑定到相应的控件 .

    这会产生:

    enter image description here

    Update:

    当你尝试我的建议时,你有一个错字,

    ...
    <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DesiredSize.Height}" />
    <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/>
    <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/>
    <RowDefinition Height="{Binding ElementName=SomeLabel, Path=DeisredSize.Height}"/>
    ...
    

    只有第一行是正确的 .

    这是 DesiredSize 不是 DeisredSize

相关问题