无法确保正确布局(WPF)

时间:2016-08-14 14:06:54

标签: c# wpf xaml

首先,这是我的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标记的页面的最小高度,垂直滚动条也不会出现

我对这一切都不熟悉,所以如果有人能就我的错误提供一些建议或发布有用资源的有用链接,我会很感激。

2 个答案:

答案 0 :(得分:1)

您可以使用网格布局

<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吗?

答案 1 :(得分:0)

显然我已经通过从页面删除MinHeight来解决它。我仍然不明白这些属性(MinHeight / MinWidth)的重点,因为窗口不尊重它们,如果添加滚动条,则完全禁用。