WPF动态加载(绑定)矢量图标

QQ图片20200522191842.png

//转换输入字符串中的任何转义字符。
System.Text.RegularExpressions.Regex.Unescape(s)
//通过替换为转义码来转义最小的字符集(\、*、+、?、|、{、[、(、)、^、$、.、# 和空白)。
//这将指示正则表达式引擎按原义解释这些字符而不是解释为元字符。
System.Text.RegularExpressions.Regex.Escape(s)

XAML中使用Converter对值进行处理
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
  <TextBlock Text="{Binding Path=Authority_icon,Converter={x:Static local:ConverterUtil.StringToIconConverter}}" Style="{StaticResource IconStyle}"/>
</StackPanel>
Converter中把值String转义
public class StringToIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                return Regex.Unescape(StringToUnicode(value.ToString()));
            }
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        /// <summary>  
        /// 字符串转为UniCode码字符串  
        /// </summary>  
        public static string StringToUnicode(string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                //这里把格式&#xe625; 转为 \ue625
                return s.Replace(@"&#x",@"\u").Replace(";","");
            }
            return s;
        }
    }
Style
<!--IconStyle-->
    <Style x:Key="IconStyle" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="/Framework;component/Fonts/#iconfont"></Setter>
        <Setter Property="TextAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="FontSize" Value="16"/>
    </Style>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 实用正则表达式匹配和替换 正则表达式非常有用,查找、匹配、处理字符串、替换和转换字符串,输入输出等。而且各种语言都...
    树幻木阅读 455评论 0 0
  • 本文摘自LTP.NET知识库。 regexp规则类包含在System.Text.RegularExpression...
    木庄阅读 234评论 0 0
  • 一、字符串在C#中,字符串是一系列不可修改的Unicode字符,创建字符串后,就不能修改它。要创建字符串,最常用的...
    CarlDonitz阅读 1,318评论 0 2
  • 1.字符串替换 例如我想把如下格式记录中的NAME值修改为WANG 修改后的字符串为 ADDR=1234;NAME...
    毕竟是秀秀啊阅读 1,070评论 0 0
  • 正则表达式:符合一定规则的表达式 作用:用于专门操作字符串。 特点:用于一些特定的符号来表示一些代码操作。这样就简...
    密奕阅读 409评论 0 0