wpf - 按钮周围的虚线边框

标签 wpf

我正在尝试在按钮周围绘制虚线边框,但没有出现边框。不知道我在这里做错了什么,你能帮忙吗?

我的 Xaml 代码:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">

    <Grid Background="Ivory">
        <Border Width="101" Height="31">
            <Border.BorderBrush>
                <VisualBrush>
                    <VisualBrush.Visual>
                        <Rectangle StrokeThickness="1" Stroke="Red" StrokeDashArray="1 2"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Border.BorderBrush>
            <Button Width="100" Height="30">
                Focus Here</Button>
        </Border>
    </Grid>
</Page>

注意:直接的问题是边框粗细,但即使添加了边框粗细,虚线边框仍然没有出现。

最佳答案

VisualBrush 的 Visual 无法自动确定其大小,因此 VisualBrush 未根据边框的大小绘制。另请注意,您需要为 Border 和 Rectangle 设置相同的 BorderThickness。看看下面的 XAML。希望它对你有好处。

<Border x:Name="MyBorderedButton" Width="101" Height="31" BorderThickness="2" >
      <Border.BorderBrush>
           <VisualBrush>
               <VisualBrush.Visual>
                   <Rectangle StrokeDashArray="4 2"
                      Stroke="Red"
                      StrokeThickness="2"
                      RadiusX="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=CornerRadius.TopRight}"
                      RadiusY="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=CornerRadius.BottomLeft}"
                      Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}"
                      Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualHeight}"/>
               </VisualBrush.Visual>
           </VisualBrush>
       </Border.BorderBrush>
       <Button>Focus Here</Button>
</Border>

它对我有用

enter image description here

关于wpf - 按钮周围的虚线边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14936002/

相关文章:

c# - UI 仍然卡住使用 backgroundWorker

c# - 鼠标左键弹起事件和openfiledialog

.net - 创建新 GUI 时,WPF 是不是 Windows 窗体的首选?

c# - 将 WPF UserControl 另存为矢量图像

wpf - SignalR Core 适用于 localhost 但不适用于 Azure 应用服务

c# - 在后台工作程序中延迟代码执行的正确方法

c# - Wpf 绑定(bind)困惑

WPF 访问路径 'C:\Users\...\Documents\My Music' 被拒绝

wpf - 如何将帮助附加到 VS 2008 中内置的 WPF 应用程序

c# - 如何在 WPF 中暂停绑定(bind)的 UI 更新?