什么是值转换器
在WPF(Windows Presentation Foundation)中,值转换器(Value Converter)是一种机制,允许你在绑定时转换绑定源和绑定目标之间的值。值转换器实现了 IValueConverter 接口,该接口包含两个方法:Convert 和 ConvertBack。这两个方法分别用于在绑定源到目标时进行值转换,以及在目标到源时进行值转换。
使用值转换器的Demo
首先创建一个绑定数据源类:
using System;
using System.ComponentModel;
namespace BindConversion
{
public class MyData : INotifyPropertyChanged
{
private DateTime _thedate;
public MyData()
{
_thedate = DateTime.Now;
}
public DateTime TheDate
{
get {
return _thedate; }
set
{
_thedate = value;
OnPropertyChanged("TheDate");
}
}
// Declare event
public event PropertyChangedEventHandler PropertyChanged;
// OnPropertyChanged method to update property value in binding
private void OnPropertyChanged(string info)
{
PropertyCh

本文详细介绍了WindowsPresentationFoundation(WPF)中的值转换器(ValueConverter),包括其实现IValueConverter接口的过程,如何在绑定源和目标间转换值,以及如何在Demo中使用MyData和MyConverter类进行不同类型的数据转换。
最低0.47元/天 解锁文章
476

被折叠的 条评论
为什么被折叠?



