原文:潜移默化学会WPF--值转换器

1. binding 后面的stringFormat的写法----连接字符串

<TextBlock Text="{Binding Path=Qty, StringFormat=Quantity: \{0\}}" />

2.

    [ValueConversion(typeof(decimal), typeof(string))]
public class PriceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
decimal price = (decimal)value;
return price.ToString("c", culture);
} public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string price = value.ToString(); decimal result;
if (Decimal.TryParse(price, System.Globalization.NumberStyles.Any, culture, out result))
{
return result;
}
return value;
}
}

用法  你懂的

<TextBox Margin="" Grid.Row="" Grid.Column="">
<TextBox.Text>
<Binding Path="UnitCost">
<Binding.Converter>
<local:PriceConverter></local:PriceConverter>
</Binding.Converter>
</Binding>
</TextBox.Text>
</TextBox>

3.条件式的值转换器

 public class PriceToBackgroundConverter : IValueConverter
{
public decimal MinimumPriceToHighlight
{
get;
set;
} public Brush HighlightBrush
{
get;
set;
} public Brush DefaultBrush
{
get;
set;
} public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
decimal price = (decimal)value;
if (price >= MinimumPriceToHighlight)
return HighlightBrush;
else
return DefaultBrush;
} public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}

用法

先引入命名空间

然后在需要转换器的窗体中定义资源

<Window.Resources>
</local:ImagePathConverter>
<local:PriceToBackgroundConverter x:Key="PriceToBackgroundConverter"
DefaultBrush="{x:Null}" HighlightBrush="Orange" MinimumPriceToHighlight="">
</local:PriceToBackgroundConverter>
</Window.Resources>

用资源

     <Border DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}"
Background="{Binding Path=UnitCost, Converter={StaticResource PriceToBackgroundConverter}}"
Padding="" >

例如 这是一个图片地址转成图片资源的一个转换器

 public class ImagePathConverter : IValueConverter
{
private string imageDirectory = Directory.GetCurrentDirectory();
public string ImageDirectory
{
get { return imageDirectory; }
set { imageDirectory = value; }
} public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string imagePath = Path.Combine(ImageDirectory, (string)value);
return new BitmapImage(new Uri(imagePath));
} public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException("The method or operation is not implemented.");
}
}

引入命名空间 ,定义资源

    <local:ImagePathConverter x:Key="ImagePathConverter"></local:ImagePathConverter>

用资源

   <Image Source="{Binding Path=ProductImagePath, Converter={StaticResource ImagePathConverter}}"
Width=""
></Image>

4. 多值转换器

  public class ValueInStockConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Return the total value of all the items in stock.
decimal unitCost = (decimal)values[];
int unitsInStock = (int)values[];
return unitCost * unitsInStock;
} public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}

5.含参数的转换器,前台页面上的一个例子的写法

  <TextBlock Grid.Column="" HorizontalAlignment="Left" Width="" ToolTip="{Binding Number}">
<TextBlock.Text>
<Binding Path="Number" Converter="{StaticResource lengthCut}">
<Binding.ConverterParameter>
<sys:String>m</sys:String>
</Binding.ConverterParameter>
</Binding>
</TextBlock.Text>
</TextBlock>

本例sys是先引入    命名空间的

xmlns:sys="clr-namespace:System;assembly=mscorlib" 

6.其他stringFormat

例如

<TextBlock Text="{Binding Date,StringFormat={}{0:MM/dd/yyyy}}" />

还有很多简单的 字母直接表示的

<TextBlock Text="{Binding UnitCost,StringFormat=The Value is {0:C}." />   内置的转换货币的转换器  例如:还有 E,P,F?等

还有1些时间的内置转换器,太多了,不想写了

有 d,D,f,F,s,M,G

潜移默化学会WPF--值转换器的更多相关文章

  1. 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据

    原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...

  2. 潜移默化学会WPF(绚丽篇)--热烈欢迎RadioButton,改造成功,改造成ImageButton,新版导航

    原文:潜移默化学会WPF(绚丽篇)--热烈欢迎RadioButton,改造成功,改造成ImageButton,新版导航 本样式 含有  触发器 和 动画    模板  ,多条件触发器,还有布局 本人博 ...

  3. 潜移默化学会WPF(转载篇)--屏幕显示Label,鼠标移上去变成textBox

    原文:潜移默化学会WPF(转载篇)--屏幕显示Label,鼠标移上去变成textBox <Window x:Class="WpfApplication1.Window1" x ...

  4. wpf值转换器IValueConverter例子

    转载:http://blog.163.com/wangzhenguo2005@126/blog/static/37140526201085113430862/ 值转换器可以把一种类型转换成另一种类型. ...

  5. WPF值转换器的使用

    <Window x:Class="CollectionBinding.MainWindow"        xmlns="http://schemas.micros ...

  6. WPF Binding值转换器ValueConverter使用简介(二)-IMultiValueConverter

    注: 需要继承IMultiValueConverter接口,接口使用和IValueConverter逻辑相同. 一.MultiBinding+Converter 多值绑定及多值转换实例 当纵向流量大于 ...

  7. WPF Binding值转换器ValueConverter使用简介(一)

    WPF.Silverlight及Windows Phone程序开发中往往需要将绑定的数据进行特定转换,比如DateTime类型的时间转换为yyyyMMdd的日期,再如有一个值是根据另外多组值的不同而异 ...

  8. 实现wpf的值转换器

    从数据库取出来的数据是1,2,3,4,5,不过要显示在控件上的,是1,2,3,4,5对应的string值,怎么办?wpf提供了很好的实现方法,那就是值转换器,我们需要做的是: 1.定义值转换类,继承I ...

  9. WPF备忘录(3)如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter

    一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...

随机推荐

  1. 【31.72%】【codeforces 604B】More Cowbell

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 浅谈java中异常抛出后代码是否会继续执行

    问题 今天遇到一个问题,在下面的代码中,当抛出运行时异常后,后面的代码还会执行吗,是否需要在异常后面加上return语句呢? public void add(int index, E element) ...

  3. 使用Perl批量读取文件最后行

    使用Perl批量读取文件最后行 面对成百上千个文件,有时我们需要查看它的最后行,单个文件打开将耗费大量时间,而通过Perl提取出最后行,将快速的帮助我们处理繁琐的事务. 特性 整个目录完全遍历,自动提 ...

  4. ENVI显示GDAL创建GeoTiff文件的一个问题及其思考

    作者:朱金灿 来源:http://blog.csdn.net/clever101 使用gdal创建一个100*100的红色的geotiff图像,代码如下: #include <assert.h& ...

  5. android Navigator的高度计算和推断是否显示

    进入互联网行业几天了, 从手机行业转到互联网行业也在慢慢的适应: IDE工具的使用(之前一直在Ubuntu 命令行进行开发). 版本号管理工具,代码架构等等这些都须要又一次适应. 好在本人另一些底子, ...

  6. js中动态创建json,动态为json添加属性、属性值的实例

    如下所示: ? 1 2 3 4 5 6 7 var param = {};  for(var i=0;i<fields.length;i++){  var field = fields[i]; ...

  7. 【u225】最优分解方案

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 经过第一轮的游戏,不少同学将会获得圣诞特别礼物,但这时细心的数学课代表发现了一个问题: 留下来的人太多 ...

  8. 支付宝开发之手机网站支付(H5支付)

    其实官方的文档中:https://docs.open.alipay.com/203 介绍的已经很详细了,我就实地的看文档操作了一遍,具体步骤如下: 一.创建应用 流程如下: 首先要有支付宝开放平台: ...

  9. [Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)

    In this lesson, we'll grab arrays of values from other arrays, resulting in a nested array. From the ...

  10. iOS BCD编码

    inline static NSData* encodeBCD(NSString *value){ //NSString *value = @"123456"; NSMutable ...