DataForm中的ComboBox(上篇)
在项目中使用DataForm时,我们经常会用到ComboxBox这个控件。然而视乎有很多值得我们注意的问题。下面以一个实例讲解下吧!
(一)实例简介:我用Employee和Department两个实体,用WCF RIA Service作为数据通信方式。
(1)实体截图:

(2)MainPage.xaml代码如下:
代码
<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,Mode=TwoWay,ElementName=departmentDomainDataSource}" DisplayMemberPath="DepartmentName" SelectedItem="{Binding Path=DepartmentID,Mode=TwoWay}" Width="200" />
</toolkit:DataField>
</StackPanel>
</DataTemplate>
</toolkit:DataForm.EditTemplate>
</toolkit:DataForm>
<ComboBox ItemsSource="{Binding Path=Data,Mode=TwoWay,ElementName=departmentDomainDataSource}" DisplayMemberPath="DepartmentName" SelectedItem="{Binding Path=DepartmentID,Mode=TwoWay}" Height="27" Width="200" />
</StackPanel>
<riaControls:DomainDataSource AutoLoad="True" LoadedData="departmentDomainDataSource_LoadedData" Name="departmentDomainDataSource" QueryName="GetDepartmentsQuery" >
<riaControls:DomainDataSource.DomainContext>
<my:EmployeeContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
<riaControls:DomainDataSource AutoLoad="True" LoadedData="employeeDomainDataSource_LoadedData" Name="employeeDomainDataSource" QueryName="GetEmployeesQuery" >
<riaControls:DomainDataSource.DomainContext>
<my:EmployeeContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
</Grid>
(3)运行截图:

(二)现象:
DataForm中的ComboBox,数据没有绑定上。而在DataForm之外的ComboBox数据绑定上了。后来我又加一个ListBox,与ComboBox绑定出现同样的问题!
(三)解决办法:
将我们要将departmentDomainDataSource放入到 <UserControl.Resources></UserControl.Resources>标记中,并且加上x:Key xaml标记。在绑定时候直接将departmentDDS作为静态资源绑定即可!就这样DataForm中的ComboBox数据就可以绑定成功了!
修改后的代码如下:
代码
<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,Mode=TwoWay,Source={StaticResource departmentDDS}}" DisplayMemberPath="DepartmentName" SelectedItem="{Binding Path=DepartmentID,Mode=TwoWay}" Width="200" />
</toolkit:DataField>
</StackPanel>
</DataTemplate>
</toolkit:DataForm.EditTemplate>
</toolkit:DataForm>
<ComboBox ItemsSource="{Binding Path=Data,Mode=TwoWay,ElementName=departmentDomainDataSource}" DisplayMemberPath="DepartmentName" SelectedItem="{Binding Path=DepartmentID,Mode=TwoWay}" Height="27" Width="200" />
</StackPanel>
<riaControls:DomainDataSource AutoLoad="True" LoadedData="employeeDomainDataSource_LoadedData" Name="employeeDomainDataSource" QueryName="GetEmployeesQuery" >
<riaControls:DomainDataSource.DomainContext>
<my:EmployeeContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
</Grid>
(四)小结:
我们在空间中嵌套需要绑定结合数据源的控件(ComboBox,ListBox,TreeView等)时,嵌套控件不能直接绑定数据源,要将数据源以Resource方式作为静态资源绑定到数据集合控件。
(五)后续的问题:
这个只是将Department绑定到了ComboBox上了,但是每个员工都是第一个部门,这个不符合我们的预计绑定!那怎样解决这个办法呢!在下面篇讲解解决这个办法!

浙公网安备 33010602011771号