wpf數據綁定 - StringFormat的妙用


寫在前面

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 ... 前加上了一些說明;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM