public
class
DateTimeConverter : IValueConverter
{
/// <summary>
/// 把时间转化成指定的格式
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object Convert( object value,
Type targetType,
object parameter,
CultureInfo culture)
{
if (value != null && value.ToString().Trim() != "" )
{
DateTime date = (DateTime)value;
// return date.ToShortDateString();
// return date.ToString("yyyy年MM月dd日 HH:mm:ss");
return date.ToString( " yyyy-MM-dd " );
}
else
{
return "" ;
}
}
public object ConvertBack( object value,
Type targetType,
object parameter,
CultureInfo culture)
{
string strValue = value.ToString();
DateTime resultDateTime;
if (DateTime.TryParse(strValue, out resultDateTime))
{
return resultDateTime;
}
return value;
}
}
/// <summary>
/// 把代码转化成指定的类容
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public class ConvtoBSfcodeToStr : IValueConverter
{
public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// if (targetType != typeof(String)) throw new InvalidOperationException("The target must be a integer!");
return (value.ToString() == " 01 " ? " 在职 " : " 离职 " );
}
public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// if (targetType != typeof(Int32)) throw new InvalidOperationException("The target must be a String!");
return (value.ToString() == " 在职 " ? " 01 " : " 02 " );
}
}
{
/// <summary>
/// 把时间转化成指定的格式
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object Convert( object value,
Type targetType,
object parameter,
CultureInfo culture)
{
if (value != null && value.ToString().Trim() != "" )
{
DateTime date = (DateTime)value;
// return date.ToShortDateString();
// return date.ToString("yyyy年MM月dd日 HH:mm:ss");
return date.ToString( " yyyy-MM-dd " );
}
else
{
return "" ;
}
}
public object ConvertBack( object value,
Type targetType,
object parameter,
CultureInfo culture)
{
string strValue = value.ToString();
DateTime resultDateTime;
if (DateTime.TryParse(strValue, out resultDateTime))
{
return resultDateTime;
}
return value;
}
}
/// <summary>
/// 把代码转化成指定的类容
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public class ConvtoBSfcodeToStr : IValueConverter
{
public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// if (targetType != typeof(String)) throw new InvalidOperationException("The target must be a integer!");
return (value.ToString() == " 01 " ? " 在职 " : " 离职 " );
}
public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// if (targetType != typeof(Int32)) throw new InvalidOperationException("The target must be a String!");
return (value.ToString() == " 在职 " ? " 01 " : " 02 " );
}
}
上面是两个自定义的类型转换器
下面是在XAML文档中的引用
一:DateTimeConverter转换器的使用
1:添加引用
xmlns:local="clr-namespace:KX_EHR"
>
2:定义
< UserControl.Resources >
< local:DateTimeConverter x:Key ="DateConverter" />
</ UserControl.Resources >
3:应用
< TextBlock Text =" {Binding Uoperatedate, Converter={StaticResource DateConverter}} " />
xmlns:local="clr-namespace:KX_EHR"
>
2:定义
< UserControl.Resources >
< local:DateTimeConverter x:Key ="DateConverter" />
</ UserControl.Resources >
3:应用
< TextBlock Text =" {Binding Uoperatedate, Converter={StaticResource DateConverter}} " />
二:ConvtoBSfcodeToStr转换器的使用
1:添加引用
xmlns:localConv="clr-namespace:KX_EHR.Class"
>
2:定义
< UserControl.Resources >
< localConv:ConvtoBSfcodeToStr x:Key ="ConverBStoStr" />
</ UserControl.Resources >
3:应用
< TextBlock Text =" {Binding 员工状态,Converter={StaticResource ConverBStoStr}} " />
xmlns:localConv="clr-namespace:KX_EHR.Class"
>
2:定义
< UserControl.Resources >
< localConv:ConvtoBSfcodeToStr x:Key ="ConverBStoStr" />
</ UserControl.Resources >
3:应用
< TextBlock Text =" {Binding 员工状态,Converter={StaticResource ConverBStoStr}} " />