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}"
/>
</datagridcomboboxcolumn></datagridtextcolumn></datagrid.columns>
</datagrid>