WPF 中如何變相讓 ListBox 寬度(Width) 100%,高度(Height) 100%,從而達到 Filled 的效果


直接貼代碼了:

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>

 

 

效果圖:

謝謝瀏覽!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM