首页 > 其他 > 详细

格式化绑定的数据

时间:2020-07-13 16:05:15      阅读:73      评论:0      收藏:0      [点我收藏+]

数据转换stringFormat

<Window x:Class="WpfApplicationDEMO.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" 
       >
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Height" Value="30"></Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <TextBlock Text="{Binding Path=Price, StringFormat={}{0:C}}" ></TextBlock>
        <TextBlock Text="{Binding Path=Price, StringFormat=The Value is {0:C}}" ></TextBlock>
        <TextBlock Text="{Binding Path=Price, StringFormat=The Value is {0:E}}"></TextBlock>
        <TextBlock Text="{Binding Path=Price, StringFormat=The Value is {0:P}}"></TextBlock>
        <TextBlock Text="{Binding Path=Price, StringFormat=The Value is {0:F2}}"></TextBlock>
        <TextBlock Text="{Binding Path=CreateDate, StringFormat=The Value is {0:d}}"></TextBlock>
        <TextBlock Text="{Binding Path=CreateDate, StringFormat=The Value is {0:D}}"></TextBlock>
        <TextBlock Text="{Binding Path=CreateDate, StringFormat=The Value is {0:f}}"></TextBlock>
        <TextBlock Text="{Binding Path=CreateDate, StringFormat=The Value is {0:F}}"></TextBlock>
        <TextBlock Text="{Binding Path=CreateDate, StringFormat=The Value is {0:s}}"></TextBlock>
        <TextBlock Text="{Binding Path=CreateDate, StringFormat=The Value is {0:M}}"></TextBlock>
        <TextBlock Text="{Binding Path=CreateDate, StringFormat=The Value is {0:G}}"></TextBlock>
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplicationDEMO
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new Product() { Name = "张三", Price = 1.8645f, CreateDate = DateTime.Now };
        }
     
    }

    public class Product
    {
        public string Name { get; set; }
        public float Price { get; set; }
        public DateTime CreateDate { get; set; }
    }
}

 

技术分享图片

 

 技术分享图片

 

 技术分享图片

 

格式化绑定的数据

原文:https://www.cnblogs.com/ZHXI/p/13292478.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!