潜移默化学会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 ...
随机推荐
- 【66.47%】【codeforces 556B】Case of Fake Numbers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [Node.js] Use Realm Object Database with Node.js
Realm is an ACID compliant object database. In this lesson, you will learn how to install Realm, def ...
- LinearLayout的一些注意事项 分类: H1_ANDROID 2013-10-26 23:01 856人阅读 评论(0) 收藏
1.orientation的默认值为horizontal,即从左向右排列.由于一般从上向下排列,所以必须指定orientation属性. 2.layout_gravity与gravity的区别: (1 ...
- [Angular] Create custom validators for formControl and formGroup
Creating custom validators is easy, just create a class inject AbstractControl. Here is the form we ...
- Strut2与Hibernate的一个web分页功能
代码没有进行过多的封装,可能看起来有点action代码部分,hibernate在这里只起到了一个查询记录集的作用. import java.util.ArrayList; import java.ut ...
- Snmp常用oid
http://blog.csdn.net/youngqj/article/details/7311849 系统参数(1.3.6.1.2.1.1) OID 描述 备注 请求方式 .1.3.6.1.2 ...
- Ubuntu安装编译OpenCV一键脚本(带ffmpeg)
1.切换到用户文件夹 cd ~ 2.新建一个文件.命名为opencv.sh 脚本例如以下: version="$(wget -q -O - http://sourceforge.net/pr ...
- 【u109】数字生成游戏(gen)
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小明完成了这样一个数字生成游戏,对于一个不包含0的数字s来说,有以下3种生成新的数的规则: 1. 将s ...
- bat文件从@含义起
今天看到一个批处理文件,内容很简单,执行很方便,学习了一下才知道就是一条条的dos命令, 掌握其中的几个常用命令能看懂别人的文件就行了 1.@ 一般紧随其后 类似@echo off 其作用类似于ech ...
- 用Canvas画一个刮刮乐
Canvas 通过 JavaScript 来绘制 2D图形.Canvas 是逐像素进行渲染的.开发者可以通过javascript脚本实现任意绘图.Canvas元素是HTML5的一部分,允许脚本语言动态 ...