代码改变世界

WPF 中如何变相让 ListBox 宽度(Width) 100%,高度(Height) 100%,从而达到 Filled 的效果

  音乐让我说  阅读(3145)  评论(0编辑  收藏  举报

直接贴代码了:

XAML:

复制代码
<Window x:Class="HelloWorld.MainWindow"
        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"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Window.Resources>
        <!-- 下面是设置让所有的 Button 控件的默认样式 -->
        <Style TargetType="{x:Type Button}">
            <Setter Property="Margin" Value="5"></Setter>
            <Setter Property="Padding" Value="5"></Setter>
            <!--<Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />-->
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/><!-- 实际高度 -->
            <RowDefinition Height="*"/><!-- 剩余高度 -->
            <RowDefinition Height="Auto"/><!-- 实际高度 -->
        </Grid.RowDefinitions>
        <Label Grid.Row="0" x:Name="label1" Content="Hello World " HorizontalAlignment="Center" VerticalAlignment="Center"
               FontSize="20" DockPanel.Dock="Top"/>

        <Grid Grid.Row="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" /><!-- 实际高度 -->
                <RowDefinition Height="Auto" /><!-- 实际高度 -->
                <RowDefinition Height="*" /><!-- 剩余高度 -->
            </Grid.RowDefinitions>

            <Button Grid.Row="0" Name="Button1" DockPanel.Dock="Top" Height="Auto" Content="Button1" />
            <Button Grid.Row="1" Name="Button2" DockPanel.Dock="Top" Height="Auto" Content="Button2" />
            <ListBox x:Name="ListBoxLog"
                    Grid.Row="2"
                    SelectionMode="Single"/>
        </Grid>
        <Button Grid.Row="3" DockPanel.Dock="Bottom"  Content="Exit" FontSize="20" />
        
    </Grid>
</Window>
复制代码

 

 

效果图:

谢谢浏览!

点击右上角即可分享
微信分享提示