wpf学习笔记 数据转换
在前面的例子里,我们做了一个显示滑块进度的小程序:
在这里有一个小小的问题是,TextBox的Text属性是string类型,而滑块的Value应该是double类型。这里.net为我们自动的做了一类型转换,但在一些复杂的逻辑中,就必须自定义一些Converter。
举个很简单的例子,我们在数据库中存储性别的时候一般会用bit类型,或男或女,双性人估计现在还不是法律能容忍的。但是1/0值反映到前台,就需要显示为男/女,这里我们用到一个SexConverter来实现。
wpf通过继承IValueConverter接口,并重写Convert与ConverBack方法,顾名思义,这两个方法一个是正向的,一个是反向,不过目前我还没有遇到用上Back的机会。
代码很简单:
xaml代码:
<Window x:Class="Data_Converter.SexConveter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Data_Converter"
Title="SexConventer" Height="300" Width="300">
<Window.Resources>
<local:BitToSexConverter x:Key="Bts"></local:BitToSexConverter>
</Window.Resources>
<StackPanel x:Name="sp_List" Background="WhiteSmoke" Margin="5">
<ListBox Name="lb_List">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="5">
<CheckBox Margin="5"></CheckBox>
<TextBlock Text="{Binding Path=Name}" Width="80" Margin="5"/>
<TextBlock Text="{Binding Path=Sex, Converter={StaticResource Bts}}" Width="200" Margin="5"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Data_Converter"
Title="SexConventer" Height="300" Width="300">
<Window.Resources>
<local:BitToSexConverter x:Key="Bts"></local:BitToSexConverter>
</Window.Resources>
<StackPanel x:Name="sp_List" Background="WhiteSmoke" Margin="5">
<ListBox Name="lb_List">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="5">
<CheckBox Margin="5"></CheckBox>
<TextBlock Text="{Binding Path=Name}" Width="80" Margin="5"/>
<TextBlock Text="{Binding Path=Sex, Converter={StaticResource Bts}}" Width="200" Margin="5"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Data_Converter
{
/// <summary>
/// SexConvet.xaml 的交互逻辑
/// </summary>
public partial class SexConveter : Window
{
public SexConveter()
{
InitializeComponent();
List<Student> lst = new List<Student>()
{
new Student(){Name="李雷",Sex=true},
new Student(){Name="韩梅梅",Sex=false},
new Student(){Name="Tom",Sex=true},
new Student(){Name="Lily",Sex=false},
};
this.lb_List.ItemsSource = lst;
}
public class Student
{
public string Name { get; set; }
public bool Sex { get; set; }
}
}
public class BitToSexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool sex = (bool)value;
if (sex) return "男";
else return "女";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Data_Converter
{
/// <summary>
/// SexConvet.xaml 的交互逻辑
/// </summary>
public partial class SexConveter : Window
{
public SexConveter()
{
InitializeComponent();
List<Student> lst = new List<Student>()
{
new Student(){Name="李雷",Sex=true},
new Student(){Name="韩梅梅",Sex=false},
new Student(){Name="Tom",Sex=true},
new Student(){Name="Lily",Sex=false},
};
this.lb_List.ItemsSource = lst;
}
public class Student
{
public string Name { get; set; }
public bool Sex { get; set; }
}
}
public class BitToSexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool sex = (bool)value;
if (sex) return "男";
else return "女";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
效果如图: