潜移默化学会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 ...
随机推荐
- 【C++竞赛 A】xxx的项链
时间限制:2s 内存限制:64MB 问题描述 xxx有一个长度为n的宝石链,宝石有m种不同的颜色.xxx想截取其中连续的一段做一个项链.为了让项链更漂亮,xxx希望项链中的宝石包含所有颜色. 输入描述 ...
- 【C++竞赛 H】The sum problem
Time Limit: 1s Memory Limit: 32MB 问题描述 Given a sequence 1,2,3,-,N, your job is to calculate the numb ...
- 如何编辑SDE数据库(转载)
转自原文 如何编辑SDE数据库(转载) 如何编辑SDE数据(转自ESRI中国社区) (2008-12-15 17:26:41) 很多刚入门的朋友对SDE数据并不太了解,接二连三的在社区里面发问,有时也 ...
- Passive DNS
http://blog.csdn.net/cnbird2008/article/details/17250707 http://netsecurity.51cto.com/art/201510/494 ...
- 如何使用Name对象,包括WorkspaceNames和DatasetNames
转自chanyinhelv原文 如何使用Name对象,包括WorkspaceNames和DatasetNames 第一原文链接 该博主还有很多有关arcgis二次开发的不错的文章. 如何使用Name对 ...
- D3.js以及通用JS(JavaScript)读取并解析server端JSON的注意事项
这个需求事实上挺明白的.可是网上搜出来的教程都乱七八糟,认为实在须要自己总结一下. D3.js眼下已经被广泛使用在数据可视化领域,随着大数据的持续发展,这个框架预计会在今后更加流行(据说其作者Mike ...
- 【hdu 2176】取(m堆)石子游戏
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- Xcode编译Undefined symbols for architecture xxx 错误总结
可能会遇到这几种错误:Undefined symbols for architecture armv7Undefined symbols for architecture armv7sUndefine ...
- NOIP模拟 最佳序列 - 二分 + 单调队列
题意: 各一个n(\(\le 20000\))的序列,定义纯洁序列为长度len满足\(L \le len \le R\)的序列,纯洁值为某一纯洁序列的平局值,输出所有纯洁序列中最大平均值. 分析: 二 ...
- ANR问题分析实例
ANR监测机制包含三种: Service ANR,前台进程中Service生命周期不能超过20秒,后台进程中Service的生命周期不能超过200秒. 在启动Service时,抛出定时消息SERVIC ...