WPF 绑定到 DataRow 列
我从 http://sweux.com/blogs/smoura/index.php/wpf/2009/06/15/wpf-toolkit-datagrid-part-iv-templatecolumns-and -row-grouping/ 提供 WPF DataGrid 中的数据分组。我正在修改示例以使用数据表而不是实体集合。
我的问题是将绑定声明 {Binding Parent.IsExpanded} 转换为适用于我的弱类型 DataTable 的内容,其中 Parent 是对具有 IsExpanded 属性的实体的引用,该声明工作正常,其中 Parent 是列并引用同一数据表中的另一个数据行。我尝试过像 {Binding Parent.Items[IsExpanded]} 和 {Binding Parent("IsExpanded")} 这样的声明,但这些似乎都不起作用。
如何创建到 DataTable 中 DataRow Parent 的 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 Col3.Item(0)}" 来显示值 r1c1,但单元格内容中没有出现任何内容。这是为什么? Item(0) 不应该只是 Col3 的另一个属性吗?
I've taken some sample code from http://sweux.com/blogs/smoura/index.php/wpf/2009/06/15/wpf-toolkit-datagrid-part-iv-templatecolumns-and-row-grouping/ that provides grouping of data in a WPF DataGrid. I'm modifying the example to use a DataTable instead of a Collection of entities.
My problem is in translating a binding declaration {Binding Parent.IsExpanded}, which works fine where Parent is a reference to an entity that has the IsExpanded attribute, to something that will work for my weakly typed DataTable, where Parent is the name of a column and references another DataRow in the same DataTable. I've tried declarations like {Binding Parent.Items[IsExpanded]} and {Binding Parent("IsExpanded")} but none of these seem to work.
How can I create a binding to the IsExpanded column of the DataRow Parent in my DataTable?
Thanks in advance,
Dave
EDIT: I've created some sample code for a generic case of this problem:
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
I've tried using the line Binding="{Binding Col3.Item(0)}" to show the value r1c1, but nothing appears in the cell contents. Why is that? Shouldn't Item(0) be just another property of Col3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此绑定表达式将绑定到设置为视图 DataContext 的对象上名为
BindingDataTable
的 DataTable 属性上名为IsExpanded
的列。不过,我明确指定了 DataTable 的第一行 (
[0]
)。我以前没有使用过 WPF DataGrid,所以我现在不确定如何获取正在绑定的当前行索引...我将看看我可以解决什么问题并更新(如果找到)。This binding expression will bind to a column named
IsExpanded
on a DataTable property namedBindingDataTable
on the object which is set as the views DataContext.However I am specifying the first row (
[0]
) of the DataTable explicitly. I've not used the WPF DataGrid before so I'm not sure at this point how to get the current row index that is being bound...I'll see what I can work out and update if found.