WPF边界上丑陋的边界

本文关键字:边界 WPF | 更新日期: 2023-09-27 18:18:50

我有一个简单的UserControl与图像和弹出。

<UserControl x:Class="Dziennik.Controls.ImageButton"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="100">
    <Button x:Name="button" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Command="{Binding Command}" CommandParameter="{Binding CommandParameter}"
            Background="Transparent"
            BorderBrush="Transparent"
            BorderThickness="0">
        <StackPanel Orientation="Horizontal">
            <Image x:Name="image" Source="{Binding ImageSource}" VerticalAlignment="Center"/>
            <Popup PlacementTarget="{Binding ElementName=button}" Placement="Bottom" IsOpen="{Binding IsMouseOver, ElementName=button,Mode=OneWay}">
                <Border BorderThickness="0" Background="#FFBEE6FD">
                    <TextBlock Text="{Binding Text}" FontWeight="Bold" FontSize="14" Margin="10,5,10,5" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </Border>
            </Popup>
        </StackPanel>
    </Button>
</UserControl>

我的问题是,我设置BorderThickness为0,但在边界有时我可以看到小的黑色边框取决于边框宽度。

我将用图片来解释我的问题。我有这个:https://i.stack.imgur.com/StVTF.png
而不是这样:https://i.stack.imgur.com/NuK2H.png


<标题>编辑:解决方案

好了,我终于找到解决办法了。我必须在弹出属性中添加allowtransparent ="True"。代码现在看起来像这样:

<UserControl x:Class="Dziennik.Controls.ImageButton"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="100">
    <Button x:Name="button" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Command="{Binding Command}" CommandParameter="{Binding CommandParameter}"
            Background="Transparent"
            BorderBrush="Transparent"
            BorderThickness="0">
        <StackPanel Orientation="Horizontal">
            <Image x:Name="image" Source="{Binding ImageSource}" VerticalAlignment="Center"/>
            <Popup PlacementTarget="{Binding ElementName=button}" Placement="Bottom" IsOpen="{Binding IsMouseOver, ElementName=button,Mode=OneWay}" AllowsTransparency="True">
                <Border BorderThickness="0" Background="#FFBEE6FD">
                    <TextBlock Text="{Binding Text}" FontWeight="Bold" FontSize="14" Margin="10,5,10,5" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                </Border>
            </Popup>
        </StackPanel>
    </Button>
</UserControl>

WPF边界上丑陋的边界

SOLUTION

好了,我终于找到解决办法了。我必须添加allowtransparent ="True"在弹出属性。代码现在看起来像这样:

<UserControl x:Class="Dziennik.Controls.ImageButton"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="100">
    <Button x:Name="button" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Command="{Binding Command}" CommandParameter="{Binding CommandParameter}"
            Background="Transparent"
            BorderBrush="Transparent"
            BorderThickness="0">
        <StackPanel Orientation="Horizontal">
            <Image x:Name="image" Source="{Binding ImageSource}" VerticalAlignment="Center"/>
            <Popup PlacementTarget="{Binding ElementName=button}" Placement="Bottom" IsOpen="{Binding IsMouseOver, ElementName=button,Mode=OneWay}" AllowsTransparency="True">
                <Border BorderThickness="0" Background="#FFBEE6FD">
                    <TextBlock Text="{Binding Text}" FontWeight="Bold" FontSize="14" Margin="10,5,10,5" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                </Border>
            </Popup>
        </StackPanel>
    </Button>
</UserControl>

…但是在边框上有时候我可以看到小的黑色边框这取决于边框宽度

您从未定义边框颜色,因此默认情况下它将是黑色(如果您没有全局样式)。您可以将BorderBrush设置为与BackgroundBrush相同的颜色以使其匹配。

如果你有时看到一个黑色的边框,即使你的厚度是0,看看SnapToDevicePixels修复它。