WPF之绑定属性值转换

1,使用Binding.Format属性简易设置绑定的属性数据显示格式。

XML 复制代码
<TextBox  Grid.Row="2" Grid.Column="1">
                    <TextBox.Text>
                        <Binding Path="UnitCost"  StringFormat="{}{0:C3}"  >                            
                        </Binding>
                    </TextBox.Text>
</TextBox>

语法解析:

StringFormat="{}{0:C3}" :第一个{}表示转义标识,StringFormat以花括号开头的需要在表达式{0:C}前加上花括号进行标识转义否则xaml无法识别{0:C},但是如果以字符开头则可以省略用于转义标识的花括号,例如Binding.StringFormat=Value{0:C},此处就进行了省略。

**显示效果:**自动添加货币符,并显示指定的3位小数位数。

常用格式字符:

2,使用实现IValueConverter的值转换类。

Binding.StringFormat={}{0:C3}同等效果的值转换类

cs 复制代码
 class CustomValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return string.Format(culture, "{0:C3}", value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return decimal.Parse(value.ToString(), NumberStyles.Any, culture);
        }
    }

在xaml中添加转换类对象。

XML 复制代码
<TextBox  Grid.Row="2" Grid.Column="1">
                    <TextBox.Text>
                        <Binding Path="UnitCost"    >
                            
                            <Binding.Converter>
                                <local:CustomValueConverter></local:CustomValueConverter>
                            </Binding.Converter>
                        </Binding>
                    </TextBox.Text>
                </TextBox>

3,使用MultiBinding.StringFormat组合多个属性。

XML 复制代码
<TextBlock Grid.Row="3" Grid.Column="1">
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0},{1}">
                            <Binding Path="ModelName"></Binding>
                            <Binding Path=" ModelNumber"></Binding>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>

显示效果:

4,使用实现IMultiValueConverter的值转换类。

实现与MultiBinding.StringFormat="{}{0},{1}"同等转换效果。

cs 复制代码
class CustomMultiValueConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            string modelName = values[0].ToString();
            string modelNumber = values[1].ToString();
            return string.Format("{0},{1}", modelName, modelNumber);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
XML 复制代码
<TextBlock Grid.Row="3" Grid.Column="1">
                    <TextBlock.Text>
                        <MultiBinding >
                            <MultiBinding.Converter>
                                <local:CustomMultiValueConverter></local:CustomMultiValueConverter>
                            </MultiBinding.Converter>
                            <Binding Path="ModelName"></Binding>
                            <Binding Path=" ModelNumber"></Binding>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
相关推荐
我好喜欢你~43 分钟前
WPF---数据模版
wpf
hqwest1 天前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
hqwest1 天前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple1 天前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
三千道应用题2 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘3 天前
开发WPF项目时遇到的问题总结
wpf
hqwest4 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars4 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest4 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest4 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf