昨天突然被问到如何在wpf里面给一段文本加个虚线外框,由于有一段时间没玩wpf了,一时还真没想出来,虽然大概有个思路,但是也不保证正确。今天回到家,闲着没事情也就随便试验了一下。

首先来个框:

    <Grid>
<Border HorizontalAlignment="Center" VerticalAlignment="Center"
Width="60" Height="30" CornerRadius="5"
BorderBrush="Blue" BorderThickness="3">
<TextBlock Text="aaa" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>

看看效果:

一个中规中矩的实线框,如何改造成我们想要的虚线框哪?

第一个想到的就是修改Border的Brush,来看看这样的xaml:

    <Grid>
<Border HorizontalAlignment="Center" VerticalAlignment="Center"
Width="60" Height="30" CornerRadius="5"
BorderThickness="3">
<Border.BorderBrush>
<LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
<LinearGradientBrush.GradientStops>
<GradientStop Color="Blue" Offset="0"/>
<GradientStop Color="Blue" Offset="0.2"/>
<GradientStop Color="Transparent" Offset="0.4"/>
<GradientStop Color="Transparent" Offset="0.6"/>
<GradientStop Color="Blue" Offset="0.8"/>
<GradientStop Color="Blue" Offset="1"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.BorderBrush>
<TextBlock Text="aaa" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>

看看效果图:

因为把Brush修改成斜线的渐变色(蓝色->透明->蓝色),因此总体上看起来就是个虚线,但是在圆角的效果取有些不怎么如意。

再换个思路,更换为使用DrawingBrush:

    <Grid>
<Border HorizontalAlignment="Center" VerticalAlignment="Center"
Width="60" Height="30" CornerRadius="5"
BorderThickness="3">
<Border.BorderBrush>
<DrawingBrush>
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Pen>
<Pen Brush="Blue" Thickness="3">
<Pen.DashStyle>
<DashStyle Dashes="3,2,0,2"/>
</Pen.DashStyle>
</Pen>
</GeometryDrawing.Pen>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,60,30" RadiusX="3" RadiusY="3"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Border.BorderBrush>
<TextBlock Text="aaa" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>

看看效果图:

利用Pen的DashStyle可以轻易的实现点划的虚线图,当然也可以轻易实现点点划的虚线,DashStyle的规则为:实线长度,空线长度,实线长度,空线长度…,而实现长度如果为0,就代表点。

不过细看这张图的话,还是会发现一些不和谐的东西,圆角从外侧看,确实是圆的,但是仔细看内侧的话,发现其内侧竟然是个直角。。。

好吧,换个思路,放弃Border了,直接用Canvas,加Rectange:

    <Grid>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="60" Height="30">
<Canvas>
<Rectangle RadiusX="5" RadiusY="5" Width="60" Height="30"
Stroke="Blue" StrokeDashArray="5,2,1,2" StrokeThickness="2"/>
</Canvas>
<TextBlock Text="aaa" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Grid>

看看效果图:

看起来还不错,确实是圆角的,不过这后面两个方案也有个明显的缺点,就是无法随着文本框内容的增长而动态的绘制。当然可以用Binding来进一步消除这个问题:

    <Grid>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="60" Height="30" x:Name="g">
<Canvas>
<Rectangle RadiusX="5" RadiusY="5"
Width="{Binding ElementName=g, Path=Width}"
Height="{Binding ElementName=g, Path=Height}"
Stroke="Blue" StrokeDashArray="5,2,1,2" StrokeThickness="2"/>
</Canvas>
<TextBlock Text="aaa" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Grid>

效果图:

看起来还不错,不过这个方案还是存在问题的,如果容器Grid本身是自增长的,那么杯具就开始了:

    <Grid>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="g">
<Canvas>
<Rectangle RadiusX="5" RadiusY="5"
Width="{Binding ElementName=g, Path=Width}"
Height="{Binding ElementName=g, Path=Height}"
Stroke="Blue" StrokeDashArray="5,2,1,2" StrokeThickness="2"/>
</Canvas>
<TextBlock Text="aaa" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Grid>

效果图:

可以发现,Grid使用了自增长的方式,Binding也只能获得错误的Width和Height,也就是0,不过别担心,WPF还提供了ActualWidth和ActualHeight:

    <Grid>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="g">
<Canvas>
<Rectangle RadiusX="5" RadiusY="5"
Width="{Binding ElementName=g, Path=ActualWidth}"
Height="{Binding ElementName=g, Path=ActualHeight}"
Stroke="Blue" StrokeDashArray="5,2,1,2" StrokeThickness="2"/>
</Canvas>
<TextBlock Margin="10,7,10,7" Text="aaa" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Grid>

效果图:

哈哈,圆满达成目标。

WPF中如何在文本外面加虚线外框的更多相关文章

  1. WPF中添加一个文本输入框,按Enter回车,执行绑定的Command

    在WPF+WMMV模式中使用键盘和鼠标事件的绑定代码如下: <TextBox x:Name="SearchBox" Text="{Binding SearchTex ...

  2. C#,WPF中使用多文本显示数据,并对其数据进行关键字高亮等操作

    需求:针对多文本信息显示,我们需要对其内容中的某些关键字或者某行进行高亮显示,并用不同颜色显示. 分析:在C#中,首先要进行多文本信息显示,可以RichTextBox(不要使用TextBox)控件,该 ...

  3. WPF中的文本度量

    关于WPF中的文本度量,需要了解以下几个问题: WPF中支持一些常用的度量单位:px(device independent pixels).in(inches).cm(centimeters).pt( ...

  4. WPF中加载高分辨率图片性能优化

    在最近的项目中,遇到一个关于WPF中同时加载多张图片时,内存占用非常高的问题. 问题背景: 在一个ListView中同时加载多张图片,注意:我们需要加载的图片分辨率非常高. 代码: XAML: < ...

  5. WPF中动态更新TextBlock文字中的超链接,文本

    1.------------------------------------------------------------------------- 修改超链接的文本文字: <TextBloc ...

  6. WPF 中的 Pack URI地(资源文件加载)

    参考资源网http://msdn.microsoft.com/zh-cn/library/aa970069.aspx#Absolute_vs_Relative_Pack_URIs 在 Windows ...

  7. WPF防止界面卡死并显示加载中效果

    原文:WPF防止界面卡死并显示加载中效果 网上貌似没有完整的WPF正在加载的例子,所以自己写了一个,希望能帮到有需要的同学 前台: <Window x:Class="WpfApplic ...

  8. WPF中动态加载XAML中的控件

    原文:WPF中动态加载XAML中的控件 using System; using System.Collections.Generic; using System.Linq; using System. ...

  9. WPF中,怎样将XAML代码加载为相应的对象?

    原文:WPF中,怎样将XAML代码加载为相应的对象? 在前面"在WPF中,如何得到任何Object对象的XAML代码?"一文中,我介绍了使用System.Windows.Marku ...

随机推荐

  1. iOS 关于自定义UICollectionViewLayout实现复杂布局

    UICollectionView的简单介绍 UICollectionView的结构 Cells Supplementary Views 追加视图 (类似Header或者Footer) Decorati ...

  2. iOS 横竖屏适配

    关于横竖屏适配 也没做过,今天读别人的源码,遇到了.为了了解清楚,就系统的学习一下. 一 横竖屏方向枚举 关于横竖屏一共有三种枚举 UIInterfaceOrientation UIInterface ...

  3. 简单的 ajax demo

    2.最重要也是最核心的是要自己改下bootstrap-paginator.js源文件,如下: [javascript] view plain copy      function oneferRepo ...

  4. curl学习总结

    1.接口    function interface($postfields=array(),$url){        //设置post请求HTTP头字段的数组        $httpheader ...

  5. Ubuntu系统vi编辑器上下左右键变ABCD的解决方法(转)

    首先卸载旧版本的vi编辑器: $sudo apt-get remove vim-common 然后安装新版vi即可: $sudo apt-get install vim Ubuntu自带有几种版本的v ...

  6. MySQL数据库(2)_MySQL数据库和数据库表操作语句

    一.关于数据库操作的sql语句 -- .创建数据库(在磁盘上创建一个对应的文件夹) create database [if not exists] db_name [character set xxx ...

  7. Swift 学习 用 swift 调用 oc

    开发过程中 很可能  把swift不成熟的地方用成熟的oc 代码来弥补一下 , 下面简单来学习一下,我也是照着视频 学习的 卖弄谈不上 就是一次学习笔记, 具体问题还是具体分析吧. 需求 给展出出来的 ...

  8. 每天一个Linux命令(45)lsof命令

        lsof命令用于查看你进程打开的文件,端口(TCP.UDP),找回/恢复删除的文件,打开文件的进程.     (1)用法:     用法:  lsof  [参数]  [文件]     (2)功 ...

  9. pppoe白皮书

    转:https://blog.csdn.net/achejq/article/details/19478811 PPPoE技术白皮书 关键词:PPP,Ethernet,PPPoE 摘    要:PPP ...

  10. com.android.dex.DexIndexOverflowException: Cannot merge new index 66299 into a non-jumbo instruction

    打包时控制台输出: Error:Execution failed for task ':app:transformClassesWithDexForAll32Release'. > com.an ...