原文:
潜移默化学会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="5" Grid.Row="2" Grid.Column="1"> <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="10"> </local:PriceToBackgroundConverter> </Window.Resources>
用资源
<Border DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}" Background="{Binding Path=UnitCost, Converter={StaticResource PriceToBackgroundConverter}}" Padding="7" >
例如 这是一个图片地址转成图片资源的一个转换器
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="100" ></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[0]; int unitsInStock = (int)values[1]; return unitCost * unitsInStock; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException(); } }
5.含参数的转换器,前台页面上的一个例子的写法
<TextBlock Grid.Column="1" HorizontalAlignment="Left" Width="60" 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