1、Converter介绍

在WPF应用程序中经常遇到类似这样的问题,在定义的类中用的bool类型的值,但是界面上某个控件的显示属性是Visibility的枚举类型的,解决这个问题可以简单在定义的类中加入一个Visibility类型的属性,将其与界面上控件的Visibility进行绑定,就可以在代码中直接操控控件的显隐了。但是采用这种方式,无可避免的让你多写了许多代码,并且功能上有重复。

幸运的是WPF已经提供了一个Convert方法,可以允许我们自定义一个Converter类,让界面的属性直接绑定你定义的类型,中间做一个类型的转换就可以实现bool到Visibility的绑定。

需求中经常有这样的问题,我输入的是一种数据类型,但是我在界面显示的完全不同的东西。这样就需要转换。WPF、Silverlight及Windows Phone程序开发中往往需要将绑定的数据进行特定转换,比如DateTime类型的时间转换为yyyyMMdd的日期,再如有一个值是根据另外多组值的不同而异的,此时我们就需要定制自己的Converter。

.Net Framework提供了两种Converter接口,单值转换的接口IValueConverter和多值转换的接口IMultiValueConverter,它们都属于System.Windows.Data命名空间,在程序集PresentationFramework.dll中。这两种值转换器都是分区域性的。其中方法Convert和ConvertBack都具有指示区域性信息的culture参数。如果区域性信息与转换无关,那么在自定义转换器中可以忽略该参数。

2、一个简单的Converter

下面的例子为bool值转换为Visibility的Converter

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace Common
{
/// <summary>
/// 布尔值转换为可见性
/// </summary>
public class BooleanToVisibilityConverter : IValueConverter
{
///当界面的绑定到DataContext中的属性发生变化时,会调用该方法,将绑定的bool值转换为界面需要的Visibility类型的值
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
} ///当界面的Visibility值发生变化时,会调用该方法,将Visibility类型的值转换为bool值返回给绑定到DataContext中的属性
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
Visibility v=value as Visibility;
if(v==null)
{
return false;
} if(v==Visibility.Visible){
return true;
}
else
return false;
}
}
}

在Xaml中使用Converter

1、引用Converter

<UserControl x:Class="View.GNSSProjectView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converter="clr-namespace:Converter"
>
<UserControl.Resources>
<converter:DoubleConvertToString x:Key="DoubleConvertToString"/>
</UserControl.Resources>
<Grid>
<StackPanel Visibility="{Binding IsVisible,Converter={StaticResource BooleanToVisibilityConverter}}"
Orientation="Horizontal" >
<TextBox MaxLength="13">
<Binding NotifyOnValidationError="True">
<Binding.ValidationRules>
<controls:DoubleValidationRule ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</TextBox>
</StackPanel>
</Grid>
</UserControl>

2、在需要使用的控件中使用Converter

 <StackPanel Visibility="{Binding IsVisible,Converter={StaticResource BooleanToVisibilityConverter}}"
Orientation="Horizontal" >
<TextBox MaxLength="13">
<Binding NotifyOnValidationError="True">
<Binding.ValidationRules>
<controls:DoubleValidationRule ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</TextBox>
</StackPanel>

3、单值转换IValueConverter

单值转换Converter详情可参考2、一个简单的Converter,这边再附上一个例子BytesToStringConverter,这是将byte数组与string互相转换的Converter

using Google.Protobuf;
using StoryEditor.Utlis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data; namespace Control
{
public class BytesToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is byte[]))
{
return null;
}
byte[] bytes = value as byte[];
string str = System.Text.Encoding.Default.GetString(bytes);
return str.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is string))
{
return null;
}
byte[] bytes = System.Text.Encoding.Default.GetBytes(value as string);
return bytes;
} }
}

在Xaml中使用Converter

<UserControl x:Class="View.EventView.SpeechShowEventView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:StoryEditor.View.EventView"
xmlns:control="clr-namespace:Control"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="350" d:DesignWidth="1000">
<UserControl.Resources>
<control:BytesToStringConverter x:Key="BytesToStringConverter"/>
</UserControl.Resources>
<Grid>
<TextBox TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible"
FontSize="14" Margin="5,10"
Text="{Binding Content,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource BytesToStringConverter}}" />
</Grid>
</UserControl>

4、多值转换IMultiValueConverter

1、Converter的代码

using System;
using System.Globalization;
using System.Windows.Data; namespace Converter
{
public class CanExportConvert : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string str1 = values[0].ToString();
string str2 = values[1].ToString();
bool? flag = (bool?)values[2];
if (flag == false && string.IsNullOrEmpty(str1))
{
return false;
}
else if (flag == true && string.IsNullOrEmpty(str2))
{
return false;
}
return true;
} public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

2、xaml中使用converter

<UserControl x:Class="View.DJIPosExportSettingView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CHC.CGO.UAV.View"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:cvt="clr-namespace:Converter"
mc:Ignorable="d"
ResizeMode="NoResize" WindowStartupLocation="CenterOwner" Header="{DynamicResource UAV_ExpoetSetting}"
Style="{StaticResource RadWindowStyle}"
MinHeight="230" MinWidth="600">
<UserControl.Resources>
<cvt:CanExportConvert x:Key="CanExportConvert"/>
</UserControl.Resources>
<Grid Margin="5">
<Button Name="exportButton"
Content="{DynamicResource UAV_Export}"
Margin="20,0,15,0"
Height="20" Width="65"
HorizontalAlignment="Right"
VerticalAlignment="Center">
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource CanExportConvert}">
<Binding Path="Text" ElementName="photoPath"/>
<Binding Path="Text" ElementName="startPhotoNumber"/>
<Binding Path="IsChecked" ElementName="checkNotMatching"/>
</MultiBinding>
</Button.IsEnabled>
</Button>
</Grid>
</UserControl>

原理与单值转换IValueConverter是一样的,注意xaml中绑定的写法即可

最新文章

  1. js刷新页面方法大全
  2. 5 Django系列之通过list_display展示多对多与外键内容在admin-web界面下
  3. [HTML/HTML5]8 使用表格
  4. Thymeleaf基本知识
  5. C# 跨线程调用控件
  6. 分享一个jquery写的类似于百度的搜索框,(可动态配置,可单列或者table格式,可填充数据)
  7. PHP PDO_MYSQL 操作类 YAF嵌入高性能类而准备
  8. hdu 1760 一道搜索博弈题 挺新颖的题目
  9. React-Native 之 redux 与 react-redux
  10. JavaWeb基础之JdbcUtils工具类final
  11. 将自己的代码托管到github上
  12. SharePoint WebService 之更新审批状态
  13. poj-3177(无向图缩点)
  14. .NET MVC 过滤器使用
  15. 【poj3623】 Best Cow Line, Gold
  16. centos设置代理上网
  17. fullfile
  18. linux sudo 系统环境变量 用户环境变量
  19. IDEA默认VIM模式
  20. C#的接口基础教程之六 接口转换

热门文章

  1. MyBatis使用模糊查询用户信息及log4j配置文件详解
  2. Supervisor操作相关的进程
  3. Redis Desktop Manager无法连接虚拟机中启动的redis服务问题排查步骤
  4. Java 多线程基础(五)线程同步
  5. [noi.ac省选模拟赛]第11场题解集合
  6. 自动完成 APP【字典树(Trie树)+dfs】
  7. Java——int、double型数组常用操作工具类
  8. Excel常用公式大全
  9. root和sudo
  10. Spark如何与深度学习框架协作,处理非结构化数据