找回密码
 立即注册

QQ登录

只需一步,快速开始

Shanlujun

新手上路

1

主题

2

帖子

27

积分

新手上路

积分
27
  • 25

    金币

  • 主题

  • 帖子

最新发帖
Shanlujun
新手上路   /  发表于:2012-2-15 20:08  /   查看:7278  /  回复:1
在WinForm下,可以将两个C1FlexGrid绑定到DataSet的某个表和这个表上的DataRelation上,方便地实现主-子数据的展现.
在WPF下,我采用相同的方法,但是在移动主表的选择行时,子表中的数据无法刷新,但ListBox可以,请问是何原因?
官方网站的 http://download.componentone.com/pub/Demo/WPF/FlexGrid/FlexGridXBAP.xbap例子中可以实现移动"当前行",但与其提供的下载代码不符,从下载的代码看,是把FlexGrid绑定到ObservableCollection的.请问如果绑定到DataTable或DataView如何实现移动"当前行"?
我使用的是VS2008+C1StudioWPF2011V3
这是xaml代码
  1. <Window x:Class="Window1"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.     Title="Window1" Height="300" Width="300" xmlns:my="clr-namespace:C1.WPF.FlexGrid;assembly=C1.WPF.FlexGrid">
  5.     <Grid Name="MainLayout">
  6.         <Grid.RowDefinitions>
  7.             <RowDefinition Height="*"/>
  8.             <RowDefinition Height="*"/>
  9.         </Grid.RowDefinitions>
  10.         <Grid.ColumnDefinitions>
  11.             <ColumnDefinition Width="*"/>
  12.             <ColumnDefinition Width="*"/>
  13.         </Grid.ColumnDefinitions>
  14.         <my:C1FlexGrid Name="MainGrid" Grid.Row="0" Grid.Column="0"/>
  15.         <my:C1FlexGrid Name="SubGrid" Grid.Row="0" Grid.Column="1"/>
  16.         <ListBox Name="MainList" Grid.Row="1" Grid.Column="0"/>
  17.         <ListBox Name="SubList" Grid.Row="1" Grid.Column="1"/>
  18.     </Grid>
  19.    
  20. </Window>
复制代码
这是后台代码
  1. Imports System.Data
  2. Imports System.Windows.Markup

  3. Class Window1

  4.     Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
  5.         Dim dtMain As New DataTable("t1"), dtSub As New DataTable("t2")
  6.         Dim ds As New DataSet

  7.         With dtMain.Columns
  8.             .Add("Id", GetType(Integer))
  9.             .Add("MainName", GetType(String))
  10.         End With
  11.         With dtSub.Columns
  12.             .Add("Id", GetType(Integer))
  13.             .Add("MainId", GetType(Integer))
  14.             .Add("SubName", GetType(String))
  15.         End With

  16.         For i As Integer = 1 To 10
  17.             Dim r1 As DataRow = dtMain.NewRow
  18.             r1("Id") = i : r1("MainName") = "MainName" &amp; i
  19.             dtMain.Rows.Add(r1)
  20.             For j As Integer = 1 To i
  21.                 Dim r2 As DataRow = dtSub.NewRow
  22.                 r2("Id") = (i - 1) * 10 + j
  23.                 r2("MainId") = i
  24.                 r2("SubName") = "MainName" &amp; i &amp; "_" &amp; "SubName" &amp; j
  25.                 dtSub.Rows.Add(r2)
  26.             Next
  27.         Next
  28.         ds.Tables.AddRange(New DataTable() {dtMain, dtSub})
  29.         ds.Relations.Add(New DataRelation("r1", dtMain.Columns("Id"), dtSub.Columns("MainId")))
  30.         MainLayout.DataContext = ds

  31.         MainGrid.SetBinding(C1.WPF.FlexGrid.C1FlexGrid.ItemsSourceProperty, New Binding("t1"))
  32.         SubGrid.SetBinding(C1.WPF.FlexGrid.C1FlexGrid.ItemsSourceProperty, New Binding("t1/r1"))
  33.         With MainList
  34.             .IsSynchronizedWithCurrentItem = True
  35.             .ItemTemplate = XamlReader.Load(<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  36.                                                 <TextBlock Text="{Binding Path=MainName}"/>
  37.                                             </DataTemplate>.CreateReader())
  38.             .SetBinding(ListBox.ItemsSourceProperty, New Binding("t1"))
  39.         End With
  40.         With SubList
  41.             .IsSynchronizedWithCurrentItem = True
  42.             .ItemTemplate = XamlReader.Load(<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  43.                                                 <TextBlock Text="{Binding Path=SubName}"/>
  44.                                             </DataTemplate>.CreateReader())
  45.             .SetBinding(ListBox.ItemsSourceProperty, New Binding("t1/r1"))
  46.         End With

  47.     End Sub

  48. End Class
复制代码

1 个回复

倒序浏览
ZenosZeng讲师达人认证 悬赏达人认证
超级版主   /  发表于:2012-2-16 14:07:00
沙发

回复 1# Shanlujun 的帖子

你好,下面代码演示了在WPF中如何进行主从数据的联动

  1. <Window x:Class="TutorialsWPF.MasterDetailBinding"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="MasterDetailBinding" Height="563" Width="946" xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml" xmlns:my="clr-namespace:TutorialsWPF">
  5.     <Grid>
  6.         <Grid.RowDefinitions>
  7.             <RowDefinition/>
  8.             <RowDefinition/>
  9.         </Grid.RowDefinitions>
  10.         <c1:C1DataSource Name="c1DataSource1" ObjectContextType="my:NORTHWNDEntities">
  11.             <c1:EntityViewSource EntitySetName="Categories" />
  12.         </c1:C1DataSource>
  13.         <!-- Master grid showing Categories. -->
  14.         <c1:C1FlexGrid AutoGenerateColumns="True" Margin="12" Name="dataGrid1" ItemsSource="{Binding ElementName=c1DataSource1, Path=Categories}"/>
  15.         <!-- Detail grid showing Products of the current Category. -->
  16.         <c1:C1FlexGrid AutoGenerateColumns="True" Margin="12" Grid.Row="1" Name="dataGrid2" ItemsSource="{Binding ElementName=c1DataSource1, Path=Categories/Products}" >
  17.             <!-- This control handler converts navigation property columns, such as Category, to lookup columns. -->
  18.             <c1:C1DataSource.ControlHandler>
  19.                 <c1:ControlHandler AutoLookup="True"/>
  20.             </c1:C1DataSource.ControlHandler>
  21.         </c1:C1FlexGrid>
  22.     </Grid>
  23. </Window>
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部