簡體   English   中英

如何在WPF的框架內制作可調整大小的頁面?

[英]How to make a resizable page inside a frame in wpf?

我想使框架內的頁面在每次調整主窗口大小時自動調整其大小。 首先,我在設計頁面時僅使用堆棧面板,然后將它們放在網格的單元格中,但是沒有用。

主窗口xmal:

<Window x:Class="WpfApplication3.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"
    xmlns:local="clr-namespace:WpfApplication3"
    mc:Ignorable="d"
    Title="MainWindow" Height="600" Width="1200">
<Grid Name="gridUI">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="69*"/>
        <ColumnDefinition Width="207*"/>
        <ColumnDefinition Width="122*"/>
        <ColumnDefinition Width="85*"/>
        <ColumnDefinition Width="35*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.4*"/>
        <RowDefinition Height="0.4*"/>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="2*"/>
    </Grid.RowDefinitions>
    <Label Background="AliceBlue" FontSize="8" HorizontalAlignment="Left" Name="doc" Margin="0,0,0,26.333" Grid.RowSpan="2">Documents</Label>
    <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1" Margin="4.667,0.333,121.565,0.333" Grid.ColumnSpan="2">
        <Button Click="NewSample_Click">New Sample</Button>
        <Button Click="CreateReport_Click">Create Report</Button>
    </StackPanel>
    <Button Grid.Row="1" Grid.Column="4" Margin="0,0.333,-0.334,0.333">Settting</Button>
    <StackPanel Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" Name="sp_doc" Margin="0,0.333,0.333,-0.333">
        <StackPanel Orientation="Horizontal" >
            <Button x:Name="sss" Click="sampleDropDown">s</Button>
            <Label FontSize="12" Name="sam">Samples</Label>

        </StackPanel>
        <StackPanel Orientation="Vertical" Name="sp_s">

        </StackPanel>
        <StackPanel Orientation="Horizontal" >
            <Button Click="reportDropDown">r</Button>
            <Label>Reports</Label>
        </StackPanel>
        <StackPanel Orientation="Vertical" Name="sp_r">

        </StackPanel>
    </StackPanel>
    <Frame x:Name="newSampleFrame" Grid.ColumnSpan="3" Content="" Grid.Column="1" HorizontalAlignment="center" Grid.Row="2" Grid.RowSpan="2" VerticalAlignment="center" Width="934" Height="456" NavigationUIVisibility="Hidden"/>
</Grid>

第xmal頁:

<Page x:Class="WpfApplication3.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:WpfApplication3"
  mc:Ignorable="d" 
  d:DesignHeight="456" d:DesignWidth="934"
  Title="Page1">

<Grid>
    <Grid.RowDefinitions>

        <RowDefinition Height="auto" />
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="5*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <!--<Border BorderBrush="Black" BorderThickness="0.5" Margin="5">-->

    <StackPanel Margin="60" Grid.Row="0" Grid.Column="0" >
        <StackPanel Orientation="horizontal" Margin="5">
            <Label Margin="0,0,100,0">Length</Label>
            <TextBox Width ="200"></TextBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="5">
            <Label Margin="0,0,105,0">Width</Label>
          <TextBox Width="200"></TextBox>    
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="5">
            <Label Margin="0,0,99,0">Weight</Label>
            <TextBox Width="200"></TextBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="5">
            <Label Margin="0,0,116,0">Age</Label>
            <TextBox Width="200"></TextBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="5">
            <Label Margin="0,0,71,0">Casting Date</Label>
            <TextBox Width="200"></TextBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="5">
            <Label Margin="0,0,73,0">Testing Date</Label>
            <TextBox Width="200"></TextBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="5">
            <Label Margin="0,0,21,0">Concrete Temperature</Label>
            <TextBox Width="200"></TextBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="5">
            <Label Margin ="0,0,91,0">Field No.</Label>
            <TextBox Width="200"></TextBox>
        </StackPanel>
        </StackPanel>
    <StackPanel  Height="30"   Width="120" Grid.Row="1" Grid.Column="1"  Orientation="Horizontal" Margin="5">
            <Button Width="50" Margin="0,0,10,0">Save</Button>
            <Button Width="50">Cancel</Button>
        </StackPanel>

    <!--</Border>-->
</Grid>

對於頁面“ WpfApplication3.Page1”,刪除

d:DesignHeight =“ 456” d:DesignWidth =“ 934”

並添加Height =“ Auto”和Width =“ Auto”。 這會起作用

您可以將DockPanel用作外部容器,並將LastChildFill設置為full。

<DockPanel LastChildFill="True">

    <Button Content="LastChildFill=True"/>
</DockPanel>

您可以將Button替換為要填充面板的控件。 另外一個好處是您可以設置其他控件停靠在另一側

<DockPanel LastChildFill="True">
    <Button Content="Dock=Top" DockPanel.Dock="Top"/>
    <Button Content="Dock=Bottom" DockPanel.Dock="Bottom"/>
    <Button Content="Dock=Left"/>
    <Button Content="Dock=Right" DockPanel.Dock="Right"/>
    <Button Content="LastChildFill=True"/>
</DockPanel>

我從這里獲得了這些示例,並且有更多信息: http : //www.wpftutorial.net/DockPanel.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM