WPF Datagrid递归绑定 [英] WPF Datagrid recursive binding

查看:101
本文介绍了WPF Datagrid递归绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想知道是否存在将DatagridComboBoxColumn的ItemsSource绑定到它所属的Datagrid的方法.

我在SQL Server数据库中有一个表是递归的,我想在带有MVVM的WPF中显示它,但在使其递归工作时遇到了一些麻烦.

在此先感谢

更多信息:

我的datagrid的itemssource绑定到用作DataContext的"MyViewModel"的属性"MyDataGridItems".

``MyDataGridItems''是类``MyModel''的Observable集合,它具有多个属性:X,Y和Z.

想法是将三个属性X,Y和Z用作我的数据网格(duh)上的列,其中X和Y是普通的文本框列,而Z作为组合框列,谁的ItemsSource应该在其中列出所有X数据网格.

我尝试使用相对来源和FindAncestor,但这给了我:

System.Windows.Data错误:4:找不到引用"RelativeSource FindAncestor,AncestorType =" System.Windows.Controls.DataGrid,AncestorLevel =" 1"的绑定源. BindingExpression:Path = MyDataGridItems.X; DataItem = null;目标元素是``DataGridComboBoxColumn''(HashCode = 13390141);目标属性为"ItemsSource"(类型为"IEnumerable")

我做错了什么?

这是XAML:

Hi All,

I was wondering if there exists a way to bind a DatagridComboBoxColumn''s ItemsSource to the Datagrid to which it belongs.

I have a table in a SQL server database which is recursive and I want to display it in WPF with MVVM, but I''m having some trouble with making it work recursively.

thanks in advance

Some more info:

My datagrid''s itemssource is bound to the property ''MyDataGridItems'' of the ''MyViewModel'' I used as my DataContext.

''MyDataGridItems'' is an Observable collection of class ''MyModel'' which has several properties: X, Y and Z.

The idea is to have the three properties X, Y and Z as columns on my datagrid (duh) with X and Y being normal textbox columns and Z as the combobox column, who''s ItemsSource should list all the X''s in the datagrid.

I have tried using Relative Source and FindAncestor, but this gives me:

System.Windows.Data Error: 4 : Cannot find source for binding with reference ''RelativeSource FindAncestor, AncestorType=''System.Windows.Controls.DataGrid'', AncestorLevel=''1''''. BindingExpression:Path=MyDataGridItems.X; DataItem=null; target element is ''DataGridComboBoxColumn'' (HashCode=13390141); target property is ''ItemsSource'' (type ''IEnumerable'')

What am I doing wrong?

Here is the XAML:

<datagrid>
        VerticalScrollBarVisibility="Auto"
        x:Name="dtgDocuments"
        ItemsSource="{Binding DocumentsList, Mode=OneWay}" 
        AutoGenerateColumns="False" 
        HorizontalAlignment="Stretch" 
        VerticalAlignment="Top"
        Padding="5">
<datagrid.columns>
	<datagridtextcolumn>
        	Header="Document Number" 
                Width="Auto" 
                Binding="{Binding DocumentNumber, Mode=TwoWay}" 
                />
	<datagridtemplatecolumn header="Document Date">
		<datagridtemplatecolumn.celltemplate>
                	<datatemplate>
                        	<textblock>
                                Text="{Binding Path=DocumentDate, StringFormat={}{0:dd/MM/yyyy}}"
                                />
                        </textblock></datatemplate>
                    </datagridtemplatecolumn.celltemplate>
	<datagridtemplatecolumn.celleditingtemplate>
		<datatemplate>
			<datepicker>
                       	SelectedDate="{Binding Path=DocumentDate, Mode=TwoWay}" DataContext="{Binding}" 
			/>
                </datepicker></datatemplate>
        </datagridtemplatecolumn.celleditingtemplate>        
        </datagridtemplatecolumn>

	<datagridcomboboxcolumn>
		Header="Related Document" 
		Width="Auto" 
		ItemsSource="{Binding ElementName=dtgDocuments, Path=DocumentNumber}"
		/> <!-- This column should list all the DocumentNumbers on the DataGrid -->
</datagridcomboboxcolumn></datagridtextcolumn></datagrid.columns>
</datagrid>

推荐答案

报价:

一种绑定DatagridComboBoxColumn的方法ItemsSource所属的Datagrid

a way to bind a DatagridComboBoxColumn''s ItemsSource to the Datagrid to which it belongs


FindAncestor绑定是否起作用?

ItemsSource ="{Binding RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type DataGrid}},Path = MyCollectionProperty}"


Does a FindAncestor binding work?

ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=MyCollectionProperty}"


您可以使用RelativeSource进行绑定就像马克写道.但是,为什么要使用AncestorLevel属性?尝试忽略它.

You can bind it using RelativeSource like Mark wrote. But, why do you use the AncestorLevel property? Try to omit it.

另一种方法是使用ElementName绑定它.只需为DataGrid设置一个名称,然后使用该名称将其绑定,如下所示:

Another way is to bind it using ElementName. Just set a name to the DataGrid and, bind to it using this name, like the following:

<DataGrid x:Name="myDataGrid">
    <DataGrid.Columns>
        <DataGridComboBoxColumn ItemsSource="{Binding ElementName=myDataGrid, Path=MyCollectionProperty}" />
    </DataGrid.Columns>
</DataGrid>


这篇关于WPF Datagrid递归绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆