2010-04-15 162 views
1

我已经从http://sweux.com/blogs/smoura/index.php/wpf/2009/06/15/wpf-toolkit-datagrid-part-iv-templatecolumns-and-row-grouping/采取了一些示例代码,它提供了WPF DataGrid中的数据分组。我正在修改示例以使用DataTable而不是实体集合。WPF绑定到DataRow列

我的问题是在翻译一个绑定声明{绑定Parent.IsExpanded},它工作正常,其中父是对具有IsExpanded属性的实体的引用,以适用于我的弱类型DataTable,其中父是列的名称并引用同一DataTable中的另一个DataRow。我尝试过{Binding Parent.Items [IsExpanded]}和{Binding Parent(“IsExpanded”)}这样的声明,但这些都不起作用。

如何在我的DataTable中创建绑定到DataRow父级的IsExpanded列?

在此先感谢, 戴夫

编辑:我已经创造了一些示例代码对于这个问题的一般情况:

Window1.xaml:

<Window x:Class="Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:WpfToolkit="http://schemas.microsoft.com/wpf/2008/toolkit" 
    Title="Window1" Height="300" Width="300"> 
    <Grid> 
     <WpfToolkit:DataGrid 
      Name="dgSampleData" 
      ItemsSource="{Binding}" 
      AutoGenerateColumns="True" 
      Margin="0,75,0,0"> 
      <WpfToolkit:DataGrid.Columns> 
       <WpfToolkit:DataGridTextColumn 
        Header="Bound Data" 
        Binding="{Binding Col3.Item(0)}" 
        /> 
      </WpfToolkit:DataGrid.Columns> 
     </WpfToolkit:DataGrid> 
    </Grid> 
</Window> 

Window1.xaml。 vb:

Imports System.Data 

Class Window1 

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded 

     Dim dtSampleData As New DataTable 
     dtSampleData.Columns.Add("Col1") 
     dtSampleData.Columns.Add("Col2") 
     dtSampleData.Columns.Add("Col3") 
     dtSampleData.Rows.Add(dtSampleData.NewRow()) 
     dtSampleData.Rows.Add(dtSampleData.NewRow()) 
     dtSampleData.Rows(0).Item(0) = "r1c1" 
     dtSampleData.Rows(0).Item(1) = "r1c2" 
     dtSampleData.Rows(0).Item(2) = dtSampleData.Rows(0) 
     dtSampleData.Rows(1).Item(0) = "r2c1" 
     dtSampleData.Rows(1).Item(1) = "r2c2" 
     dtSampleData.Rows(1).Item(2) = dtSampleData.Rows(0) 
     dgSampleData.DataContext = dtSampleData 

    End Sub 

End Class 

我试过使用行Binding =“{Binding Co l3.Item(0)}“显示值r1c1,但没有任何内容出现在单元格中。这是为什么?不应该项目(0)只是Col3的另一个属性?

回答

1

此绑定表达式将绑定到名为IsExpanded的DataTable属性上名为BindingDataTable的对象被设置为视图DataContext。

<CheckBox IsChecked="{Binding BindingDataTable.(Rows)[0][IsExpanded]}" 
      Content="Test"></CheckBox> 

但是我明确指定了DataTable的第一行([0])。我之前没有使用WPF DataGrid,所以我不确定在这一点上如何获取当前绑定的行索引...我会看看我可以制定和更新如果找到。

+0

谢谢你这个西蒙。每行都有一个名为Parent的列,它是对DataTable中其他行之一的引用。这有帮助吗? – Trindaz 2010-04-15 03:09:08

+0

@Trindaz你想要将Parent.IsExpandable值绑定到什么?通过绑定属性的某种类型的'DataGridColumn'? – 2010-04-15 04:29:29