WPF 在底部和右侧显示边框
WPF is showing a border at the bottom and on the right
我目前遇到的问题是 WPF 在 window 的底部和右侧显示一条黑线。
在 window 中只有一个堆栈面板 Margin
设置为 0
。这是我的 Window 配置:
<Window x:Class="test.Settings"
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:test"
mc:Ignorable="d"
Title="test"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize"
Background="White"
Margin="0"
Padding="0">
正如您在图片上看到的那样,有一条小黑线。有谁知道如何删除它?
这是完整的xaml
<Window x:Class="TelefonPopup.Settings"
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:TelefonPopup"
mc:Ignorable="d"
Title="Telefonpopup Settings"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize">
<Window.Resources>
<Style TargetType="Label">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="FontSize" Value="16"/>
</Style>
<Style TargetType="RadioButton">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="10, 0"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label>Eingeschaltet:</Label>
<StackPanel Orientation="Horizontal" Grid.Column="1">
<RadioButton x:Name="rbtnOn">EIN</RadioButton>
<RadioButton x:Name="rbtnOff">AUS</RadioButton>
</StackPanel>
<Label Grid.Row="1">Zeit:</Label>
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1">
<TextBox x:Name="tbTime" VerticalAlignment="Center" FontSize="16" Margin="15,0,0,0"></TextBox>
<Label>s</Label>
</StackPanel>
<Button x:Name="btnSave" Click="btnSave_Click" Margin="45,10" FontSize="14" Padding="3" Grid.Row="2" Grid.ColumnSpan="2">Speichern</Button>
</Grid>
</Window>
在使用 SizeToContent="WidthAndHeight"
和 ResizeMode="None"
之前,我已经 运行 解决过这个问题。由于内部数学,这是 WPF 中的布局渲染故障问题。
您可以通过将 SnapsToDevicePixels="True"
添加到您的 Window
来轻松解决它。
SnapsToDevicePixels
将确保元素在渲染过程中绘制在像素边界上。
另一种选择是使用 UseLayoutRounding="True"
,它在测量和排列过程中发挥作用。
我强烈建议您 read-up 这两个属性,然后决定哪个最适合您的应用。
我目前遇到的问题是 WPF 在 window 的底部和右侧显示一条黑线。
在 window 中只有一个堆栈面板 Margin
设置为 0
。这是我的 Window 配置:
<Window x:Class="test.Settings"
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:test"
mc:Ignorable="d"
Title="test"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize"
Background="White"
Margin="0"
Padding="0">
正如您在图片上看到的那样,有一条小黑线。有谁知道如何删除它?
这是完整的xaml
<Window x:Class="TelefonPopup.Settings"
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:TelefonPopup"
mc:Ignorable="d"
Title="Telefonpopup Settings"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize">
<Window.Resources>
<Style TargetType="Label">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="FontSize" Value="16"/>
</Style>
<Style TargetType="RadioButton">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="10, 0"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label>Eingeschaltet:</Label>
<StackPanel Orientation="Horizontal" Grid.Column="1">
<RadioButton x:Name="rbtnOn">EIN</RadioButton>
<RadioButton x:Name="rbtnOff">AUS</RadioButton>
</StackPanel>
<Label Grid.Row="1">Zeit:</Label>
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1">
<TextBox x:Name="tbTime" VerticalAlignment="Center" FontSize="16" Margin="15,0,0,0"></TextBox>
<Label>s</Label>
</StackPanel>
<Button x:Name="btnSave" Click="btnSave_Click" Margin="45,10" FontSize="14" Padding="3" Grid.Row="2" Grid.ColumnSpan="2">Speichern</Button>
</Grid>
</Window>
在使用 SizeToContent="WidthAndHeight"
和 ResizeMode="None"
之前,我已经 运行 解决过这个问题。由于内部数学,这是 WPF 中的布局渲染故障问题。
您可以通过将 SnapsToDevicePixels="True"
添加到您的 Window
来轻松解决它。
SnapsToDevicePixels
将确保元素在渲染过程中绘制在像素边界上。
另一种选择是使用 UseLayoutRounding="True"
,它在测量和排列过程中发挥作用。
我强烈建议您 read-up 这两个属性,然后决定哪个最适合您的应用。