我对 WPF 很陌生,我想创建一个模式弹出设置管理器。我的客户可以以不同的分辨率使用我的应用程序。我还想稍后扩展我的设置管理器并添加更多字段(文本框等)。我已经创建了它的基本版本,但我不确定我是否在设计布局时遵循了所有最佳实践(面板类型、元素的宽度/高度等)。我附上了我正在尝试实现的屏幕截图,并且我还附上了 XAML 与这篇文章。如果我可以做任何更改来改进布局,我将非常感谢您的反馈(以使其更易于维护,因为我将很快添加更多这样的表单)。
注意:我知道我现在已经硬编码文本(不支持本地化等),但现在我只关心布局。我真的很感谢你的帮助。
<Window.Resources>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Width="350" VerticalAlignment="Top" Grid.Row="0" FontFamily="Microsoft Sans Serif" FontSize="11" Foreground="#FFFEFFFC" Text="This is the header containing information related to my dialogbox. It can be multiple lines aswell." Background="{StaticResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" TextWrapping="Wrap" Grid.ColumnSpan="3" Padding="5" />
<GroupBox Grid.Row="1" Header="My Group Box" Margin="5" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock VerticalAlignment="Center" Text="My Label1" />
<Label DockPanel.Dock="Right" VerticalAlignment="Center" Foreground="Crimson" Content="*" />
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Vertical">
<TextBox TextWrapping="Wrap" Margin="0,0,0,0" MaxLength="255" />
<TextBlock TextWrapping="Wrap" Visibility="Visible" Margin="0,2,0,0" Name="labelError1" VerticalAlignment="Top" FontSize="10" Foreground="#FFD80000" Text="Error Message for my textbox1" MaxWidth="455"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,10,0,0">
<TextBlock VerticalAlignment="Center" Text="My Label 2" />
<Label DockPanel.Dock="Right" VerticalAlignment="Center" Foreground="Crimson" Content="*"/>
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Vertical">
<TextBox TextWrapping="Wrap" Margin="0,0,0,0" MaxLength="255" />
<TextBlock TextWrapping="Wrap" Visibility="Visible" Margin="0,2,0,0" Name="label1" VerticalAlignment="Top" FontSize="10" Foreground="#FFD80000" Text="Error Message for my textbox2" MaxWidth="455"/>
</StackPanel>
</Grid>
</GroupBox>
<StackPanel Grid.Row="2" Orientation="Horizontal" Margin="0,10,10,10" HorizontalAlignment="Right" >
<Button Name="OkButton" Width="88" Height="28" IsDefault="True" Content="OK" Command="{Binding Path=OkCommand, Mode=OneWay}" HorizontalAlignment="Right" Margin="0,0,6,0"/>
<Button Name="CancelButton" Width="88" Height="28" IsCancel="True" Content="Cancel" HorizontalAlignment="Left" Margin="0"/>
</StackPanel>
</Grid>
<Window xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="My Settings Manager"
SizeToContent="Height"
Background="#ffe6e6e6"
MinWidth="350"
MinHeight="290"
Width="350"
>
<Window.Resources>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Top" Grid.Row="0" FontFamily="Microsoft Sans Serif" FontSize="11" Foreground="#FFFEFFFC" Text="This is the header containing information related to my dialogbox. It can be multiple lines aswell." Background="{StaticResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" TextWrapping="Wrap" Grid.ColumnSpan="3" Padding="5" />
<GroupBox Grid.Row="1" Header="My Group Box" Margin="5" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="10"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Text="My Label1" />
<TextBlock Grid.Row="0" Grid.Column="1" Foreground="Crimson" Text="*" Margin="4,2,0,0"/>
<TextBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" TextWrapping="Wrap" Margin="0,0,0,0" MaxLength="255" />
<TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="0,2,0,0" Name="labelError1" VerticalAlignment="Top" FontSize="10" Foreground="#FFD80000" Text="Error Message for my textbox1" />
<TextBlock Grid.Row="4" Grid.Column="0" VerticalAlignment="Center" Text="My Label1" />
<TextBlock Grid.Row="4" Grid.Column="1" Foreground="Crimson" Text="*" Margin="4,2,0,0"/>
<TextBox Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" TextWrapping="Wrap" Margin="0,0,0,0" MaxLength="255" />
<TextBlock Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="2" Margin="0,2,0,0" Name="labelError2" VerticalAlignment="Top" FontSize="10" Foreground="#FFD80000" Text="Error Message for my textbox1" />
</Grid>
</GroupBox>
<Button Grid.Row="2" Name="OkButton" Width="88" IsDefault="True" Content="OK" Padding="0,3,0,3" Margin="0,5,5,5" HorizontalAlignment="Right"/>
<Button Grid.Row="2" Name="CancelButton" Width="88" IsCancel="True" Content="Cancel" Margin="0,5,100,5" Padding="0,3,0,3" HorizontalAlignment="Right"/>
</Grid>
</Window>