无法看到正确的布局(WPF)

本文关键字:布局 WPF | 更新日期: 2023-09-27 18:02:31

首先,这里是我的XAML标记(ModifySpDefaultValues.xaml):

<Page x:Class="EMS_WPF_Test.ModifySpDefaultValues"
  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:EMS_WPF_Test"
  mc:Ignorable="d" d:DesignWidth="360" Title="ModifyPvDefaultValues" Style="{DynamicResource Form}" d:DesignHeight="360" MinHeight="360" MinWidth="360">
<ScrollViewer Margin="10" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <DockPanel>
        <StackPanel DockPanel.Dock="Left">
            <Label Content="Min/max" Style="{DynamicResource Label}"/>
            <Label Content="Minimum allowed sp:" Style="{DynamicResource Label}"/>
            <Label Content="Maximum allowed sp:" Style="{DynamicResource Label}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Content="Alarm values" Style="{DynamicResource Label}"/>
            <Label Content="Period:" Style="{DynamicResource Label}"/>
            <Label Content="Min Pv:" Style="{DynamicResource Label}"/>
            <Label Content="Max Pv:" Style="{DynamicResource Label}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Content="Local setpoint" Style="{DynamicResource Label}"/>
            <Label Content="Default setpoint:" Style="{DynamicResource Label}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Content="External setpoint" Style="{DynamicResource Label}"/>
            <Label Content="Sp addition:" Style="{DynamicResource Label}"/>
            <Label Content="Default manual setpoint:" Style="{DynamicResource Label}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label x:Name="GoBack" Content="Back" Style="{DynamicResource Hyperlink}" MouseLeftButtonUp="GoBack_MouseLeftButtonUp"/>
        </StackPanel>
        <StackPanel DockPanel.Dock="Right">
            <Label Style="{DynamicResource Label}"/>
            <Label Content="What's this?" Style="{DynamicResource Hyperlink}"/>
            <Label Content="What's this?" Style="{DynamicResource Hyperlink}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Content="What's this?" Style="{DynamicResource Hyperlink}"/>
            <Label Content="What's this?" Style="{DynamicResource Hyperlink}"/>
            <Label Content="What's this?" Style="{DynamicResource Hyperlink}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Content="What's this?" Style="{DynamicResource Hyperlink}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Content="What's this?" Style="{DynamicResource Hyperlink}"/>
            <Label Content="What's this?" Style="{DynamicResource Hyperlink}"/>
        </StackPanel>
        <StackPanel>
            <Label Style="{DynamicResource Label}"/>
            <TextBox Style="{DynamicResource TextBox}"/>
            <TextBox Style="{DynamicResource TextBox}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Style="{DynamicResource Label}"/>
            <TextBox Style="{DynamicResource TextBox}"/>
            <TextBox Style="{DynamicResource TextBox}"/>
            <TextBox Style="{DynamicResource TextBox}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Style="{DynamicResource Label}"/>
            <TextBox Style="{DynamicResource TextBox}"/>
            <Label Style="{DynamicResource Label}"/>
            <Label Style="{DynamicResource Label}"/>
            <TextBox Style="{DynamicResource TextBox}"/>
            <TextBox Style="{DynamicResource TextBox}"/>
        </StackPanel>
    </DockPanel>
</ScrollViewer>

XAML for window(加载以上页面):

<NavigationWindow x:Class="EMS_WPF_Test.Window1"
    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:EMS_WPF_Test"
    mc:Ignorable="d"
    Title="Modify device" MinHeight="300" MinWidth="680" Height="300" Width="680" Source="ModifySpDefaultValues.xaml" WindowStartupLocation="CenterScreen" ShowsNavigationUI="False">

样式:

<Application x:Class="Test.App"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 StartupUri="MainWindow.xaml">
<Application.Resources>
    <!-- Header text style -->
    <Style x:Key="Hyperlink">
        <Setter Property="Label.Margin" Value="0,0,25,0"></Setter>
        <Setter Property="Label.Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}"></Setter>
        <Setter Property="Label.Cursor" Value="Hand"></Setter>
        <Setter Property="Label.Height" Value="20"></Setter>
        <Setter Property="Label.Padding" Value="0"/>
    </Style>
    <Style x:Key="OperationMethodBox">
        <Setter Property="Label.Margin" Value="0,0,25,0"></Setter>
        <Setter Property="ComboBox.Width" Value="300"></Setter>
        <Setter Property="Label.Height" Value="20"></Setter>
        <Setter Property="Label.Padding" Value="0"/>
    </Style>
    <Style x:Key="TextBox">
        <Setter Property="Label.Margin" Value="0,0,25,0"></Setter>
        <Setter Property="TextBox.MinWidth" Value="50"></Setter>
        <Setter Property="Label.Height" Value="20"></Setter>
        <Setter Property="Label.Padding" Value="0"/>
    </Style>
    <Style x:Key="Label">
        <Setter Property="Label.Height" Value="20"/>
        <Setter Property="Panel.Margin" Value="0,0,25,0"/>
        <Setter Property="Label.Padding" Value="0"/>
    </Style>
    <Style x:Key="Form">
        <Setter Property="Page.Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
    </Style>
</Application.Resources>

我是WPF的新手,它可能会显示。我不能让滚动条像我想的那样工作。由于某些原因,即使我的窗口的高度小于上面展示的带有XAML标记的页面的最小高度,垂直滚动条也不会出现

我对这一切都很陌生,所以如果有人能给我一些建议,告诉我做错了什么,或者发布有用的资源链接,我将不胜感激。

无法看到正确的布局(WPF)

你可以使用网格布局

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0"/>
    <StackPanel Grid.Column="1"/>
    <StackPanel Grid.Column="2"/>
</Grid>

第一列和第三列将占用所需的空间,第二列将占用其余的空间。

对于Scrollbars:这实际上应该工作,你可以为你的Frame添加XAML吗?

显然我已经通过从页面中删除MinHeight解决了这个问题。我仍然不明白这些属性的点(MinHeight/MinWidth)看到的窗口不尊重他们和滚动条完全禁用,如果添加