当前位置: 移动技术网 > IT编程>开发语言>.net > wpf数据绑定 - StringFormat的妙用

wpf数据绑定 - StringFormat的妙用

2019年02月20日  | 移动技术网IT编程  | 我要评论

游客止步快播,2013070,多美滋妈妈奶粉

写在前面

wpf中常常有这样的情况:需要在ui上显示一些信息,比如显示一张图片的信息,信息结构是:

  图片名:xxx
  图片尺寸:xxx              

而其中的 xxx 通常通过数据绑定来获得, xxx 前面的内容是需要在xaml中写死的,这个时候如何布局比较方便呢?
可以使用stringformat来简单实这个需求.


stringformat的使用

看下面的代码示例:

<textbox margin="5" grid.row="2" grid.column="1"
         text="(bindingpath=unitcost,stringformat={}(o:c})">  
</textbox>

这段代码初步演示了如何使用stringformat格式字符串.
下面的图中展示了一些常用的字符串格式:


下面展示一下如何使用stringformat解决开头提到的需求;
假如在textblock的text绑定了后台的一字符串来展示信息,如果希望在该字符串的前后加上一些提示或者后缀什么的,可以像下面这样来写:

<textblock text="binding path=name,stringformat={}xxxx{0}xxxx"/>

上面的代码中xxxx就是我们可以随意添加的文字内容,前面就会被展示在绑定的内容的前面,后面的就在后面


下面通过一个demo来说明一下

  • 我使用vs2017建立一个项目:stringformatdemo
  • 项目目录如下图:
  • 简单的user类的代码:


    user

    namespace stringformatdemo
    {
        public class user
        {
            public string firstname { get; set; }
            public string lastname { get; set; }
            public int age { get; set; }
            public string sex { get; set; }
        }
    }
    

  • appvm类的代码


    appvm

    using microsoft.practices.prism.viewmodel;
    namespace stringformatdemo
    {
        class appvm:notificationobject
        {
            public appvm()
            {
                user = new user
                {
                    firstname = "la",
                    lastname = "laggage",
                    sex = "男",
                    age = 20
                };
            }
    
            private user _user;
            public user user
            {
                get => _user;
                set
                {
                    if (value == _user) return;
                    _user = value;
                    raisepropertychanged(nameof(user));
                }
            }
    
        }
    }

  • 前台xaml


    前台xaml

    <window x:class="stringformatdemo.mainwindow"
        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:stringformatdemo"
        mc:ignorable="d" foreground="black" fontsize="16"
        title="mainwindow" height="450" width="800">
    <window.datacontext>
        <local:appvm/>
    </window.datacontext>
    <grid>
        <border height="150" width="240">
            <stackpanel>
                <stackpanel.resources>
                    <style targettype="textblock">
                        <setter property="padding" value="8"></setter>
                    </style>
                </stackpanel.resources>
                <textblock foreground="black" text="{binding path=user.firstname,stringformat={}firstname: {0}}"/>
                <textblock text="{binding path=user.lastname,stringformat={}lastname: {0}}"/>
                <textblock text="{binding path=user.sex,stringformat={}sex: {0},fallbackvalue=failed}"/>
                <textblock text="{binding path=user.age,stringformat={}age: {0}}"/>
            </stackpanel>
        </border>
    </grid>
    </window>

这个demo非常非常非常的简单,mainwindow的xaml中实例化appvm作为datacontext,然后在stackpannel中绑定了appvm下的user属性,并显示user的firstname,lastname ... 最终效果如下:

通过stringformat成功在 firstname lastname ... 前加上了一些说明;

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网