潜移默化学会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--值转换器的更多相关文章
- 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据
原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...
- 潜移默化学会WPF(绚丽篇)--热烈欢迎RadioButton,改造成功,改造成ImageButton,新版导航
原文:潜移默化学会WPF(绚丽篇)--热烈欢迎RadioButton,改造成功,改造成ImageButton,新版导航 本样式 含有 触发器 和 动画 模板 ,多条件触发器,还有布局 本人博 ...
- 潜移默化学会WPF(转载篇)--屏幕显示Label,鼠标移上去变成textBox
原文:潜移默化学会WPF(转载篇)--屏幕显示Label,鼠标移上去变成textBox <Window x:Class="WpfApplication1.Window1" x ...
- wpf值转换器IValueConverter例子
转载:http://blog.163.com/wangzhenguo2005@126/blog/static/37140526201085113430862/ 值转换器可以把一种类型转换成另一种类型. ...
- WPF值转换器的使用
<Window x:Class="CollectionBinding.MainWindow" xmlns="http://schemas.micros ...
- WPF Binding值转换器ValueConverter使用简介(二)-IMultiValueConverter
注: 需要继承IMultiValueConverter接口,接口使用和IValueConverter逻辑相同. 一.MultiBinding+Converter 多值绑定及多值转换实例 当纵向流量大于 ...
- WPF Binding值转换器ValueConverter使用简介(一)
WPF.Silverlight及Windows Phone程序开发中往往需要将绑定的数据进行特定转换,比如DateTime类型的时间转换为yyyyMMdd的日期,再如有一个值是根据另外多组值的不同而异 ...
- 实现wpf的值转换器
从数据库取出来的数据是1,2,3,4,5,不过要显示在控件上的,是1,2,3,4,5对应的string值,怎么办?wpf提供了很好的实现方法,那就是值转换器,我们需要做的是: 1.定义值转换类,继承I ...
- WPF备忘录(3)如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter
一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...
随机推荐
- ios开发核心动画五:图标抖动效果--CAKeyframeAnimation
#import "ViewController.h" #define angle2Rad(angle) ((angle) / 180.0 * M_PI) @interface Vi ...
- SpringMVC+Spring+Mybatis+Mysql项目搭建
眼下俺在搭建一个自己的个人站点玩玩.一边练习.一边把用到的技术总结一下,日后好复习. 站点框架大致例如以下图所看到的: 眼下仅仅用到了SpringMVC+Spring+Mybatis+Mysql.把它 ...
- 【54.08%】【BZOJ 1941】Hide and Seek
Time Limit: 16 Sec Memory Limit: 162 MB Submit: 919 Solved: 497 [Submit][Status][Discuss] Descript ...
- ios开发事件处理之:一:UIView的拖拽
1.ios当中常⽤的事件? 触摸事件 ,加速计事件 ,远程控制事件 2.什么是响应者对象? 继承了UIResponds的对象我们称它为响应者对象 UIApplication.UIViewContro ...
- 谈谈JavaEE的mvc模式及典型的三层架构
首先,向读者介绍一下mvc架构,mvc是一种源于桌面程序的架构模式,它的基本思想是把程序界面和业务逻辑分开,这样便于软件的后期维护,同时也方便开发时期分工及管理,mvc有很多有点所以现在已经被广泛的应 ...
- [Javascript] Understand Function Composition By Building Compose and ComposeAll Utility Functions
Function composition allows us to build up powerful functions from smaller, more focused functions. ...
- [React] Understand React.Children Utilities
The data contained in this.props.children is not always what you might expect. React provides React. ...
- Android开发和測试实践 - 接入友盟统计
这两年一直在做无线的測试,兴许还会继续去做无线的測试,可是之前由于时间的原因一直都没有非常细致的了解到代码层面. 最近抽出时间自己做了些app的开发,决定假设想把移动的測试做好做深入.有一定的app开 ...
- show binlog events 命令查看某个binlog日志内容
mysql> show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]; 选项解析: IN 'l ...
- MySQL中关于OR条件的优化
转载 MySQL在 5.0版本中引入新特性:索引合并优化(Index merge optimization),当查询中单张表可以使用多个索引时,同时扫描多个索引并将扫描结果进行合并. 该特新主要应用于 ...