潜移默化学会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 ...
随机推荐
- node-sass的安装问题
1.认识node-sass 我觉得要解决node-sass的问题,你首先至少要简单的了解node-sass是个什么东西?为什么要安装它? 对于在项目中使用sass的语法的时候,需要通过sass-loa ...
- Seagate-保修验证(za25shrx)
保修验证 http://support.seagate.com/customer/zh-CN/warranty_validation.jsp Seagate 保修验证 End User ...
- 【22.17%】【codeforces718B】 Efim and Strange Grade
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Redis Service
https://raw.githubusercontent.com/MSOpenTech/redis/3.0/Windows%20Service%20Documentation.md
- oracle 内存结构具体解释
Oracle 内存结构 与 Oracle 实例关联的基本内存结构包含: 系统全局区 (SGA):由全部server和后台进程共享.SGA 中存储的数据演示样例包含快速缓存的数据块和共享 SQL 区域. ...
- [TypeScript] Create a fluent API using TypeScript classes
You can create an easy to chain API using TypeScript classes. Learn about the thisreturn type annota ...
- C#中的“静态”
静态构造函数: C#的一个新特征是也能够给类编写无參数的静态构造函数. 编写静态构造函数的一个原因是,类有一些静态字段或属性,须要在第一次使用类之前.从外部源中初始化这些静态的字段和属性. .NET运 ...
- js实现表格配对小游戏
js实现表格配对小游戏 一.总结 一句话总结: 二.js实现表格配对 1.配对游戏案例说明 实例描述: 当用户点击两个相同的图案或字符后配对成功,全部配对成功后游戏获胜 案例008采用了大家常见的小游 ...
- matlab 实现 stacked Autoencoder 解决图像分类问题
Train Stacked Autoencoders for Image Classification 1. 加载数据到内存 [train_x, train_y] = digitTrainCellAr ...
- DotNetty编写跨平台网络通信
DotNetty编写跨平台网络通信 长久以来,.Net开发人员都非常羡慕Java有Netty这样,高效,稳定又易用的网络通信基础框架.终于微软的Azure团队,使用C#实现的Netty的版本发布.不但 ...