本篇继续上篇内容说: 除了上篇的一个问题,其实还有一个问题:就是当我们改选部门时也会报错。如下所述:
(一)现象:
当我们选择其他的部门的时候,就会出现“输入类型不正确”,错误截图如下:
(二)分析及解决方法:
这是由于SelectedItem是Department类型,而DepartmentID是int类型。添加一个类型转换器DepartementConverter,并在SelectedItem中加上Converter={StaticResource departmentConverter}实现了Department<=>DepartmentID类型互换,类型转换器代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System.Windows.Data;
using System.Linq;
using ComboxInDataForm.Web;
namespace ComboxInDataForm.Converter
{
public class DepartementConverter:IValueConverter
{
public DomainDataSource ItemsSource { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int Id = (int)value;
var returnValue = ItemsSource.Data.Cast < Department > ().Where(item => item.DepartmentID == Id).FirstOrDefault();
return returnValue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Department objectEntity = value as Department;
int returnValue = 0;
if (objectEntity != null)
{
returnValue = objectEntity.DepartmentID;
}
return returnValue;
}
}
}
这种方式绑定后台不需要写任何代码,只需要在前台中写,MainPage.xaml代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
< riaControls:DomainDataSource AutoLoad = " True " x:Key = " departmentDDS " LoadedData = " departmentDomainDataSource_LoadedData " QueryName = " GetDepartmentsQuery " >
< riaControls:DomainDataSource.DomainContext >
< my:EmployeeContext />
</ riaControls:DomainDataSource.DomainContext >
</ riaControls:DomainDataSource >
< local:DepartementConverter x:Key = " departmentConverter " ItemsSource = " {StaticResource departmentDDS} " />
</ UserControl.Resources >
< Grid x:Name = " LayoutRoot " Background = " White " >
< StackPanel >
< toolkit:DataForm Name = " dataForm1 " VerticalAlignment = " Top " Width = " 400 " ItemsSource = " {Binding Path=Data,ElementName=employeeDomainDataSource} " >
< toolkit:DataForm.EditTemplate >
< DataTemplate >
< StackPanel >
< toolkit:DataField Label = " 员工编号: " >
< TextBox Text = " {Binding EmployeeID,Mode=TwoWay} " Width = " 200 " />
</ toolkit:DataField >
< toolkit:DataField Label = " 员工姓名: " >
< TextBox Text = " {Binding EmployeeName,Mode=TwoWay} " Width = " 200 " />
</ toolkit:DataField >
< toolkit:DataField Label = " 所属部门 " >
< ComboBox ItemsSource = " {Binding Path=Data,Source={StaticResource departmentDDS}} " DisplayMemberPath = " DepartmentName " SelectedItem = " {Binding Path=DepartmentID,Mode=TwoWay,Converter={StaticResource departmentConverter}} " Width = " 200 " />
</ toolkit:DataField >
</ StackPanel >
</ DataTemplate >
</ toolkit:DataForm.EditTemplate >
</ toolkit:DataForm >
</ StackPanel >
< riaControls:DomainDataSource AutoLoad = " True " LoadedData = " employeeDomainDataSource_LoadedData " Name = " employeeDomainDataSource " QueryName = " GetEmployeesQuery " >
< riaControls:DomainDataSource.DomainContext >
< my:EmployeeContext />
</ riaControls:DomainDataSource.DomainContext >
</ riaControls:DomainDataSource >
</ Grid >
(三) 小结:
当我们利用相互转换的类型转换器后,上篇的问题就已经解决了!类型转换器起到很重要的作用!
(四) 后续问题:
当我们点下一个员工:在回到上一个员工,会发现上个员工部门发生了错位,这个应该是DataForm的一个Bug吧!如下图:
解决办法:
(1)不使用DataForm的分页功能,而是用DataGrid(选中行)+DataForm(单条记录)实现分页
(2)重写ComboBox,添加 SelectedValue的自定义ComboBox
(3)第三方控件