Ancestor Datacontext Linkage

It is best to show you my code first and then ask a question:

<Window DataContext="{Binding RelativeSource={RelativeSource Self}}"> <Grid> <DataGrid ItemsSource="{Binding Printers}" > <DataGrid.Columns> <DataGridComboBoxColumn Header="Drucker Typ" ItemsSource="{Binding Relative" SelectedItemBinding="{Binding PrinterType, Mode=TwoWay}" Width="Auto" ItemsSource="{Binding}" /> </DataGrid.Columns> </DataGrid> </Window> 

I have a DataGridComboBoxColumn and you want to bind the ItemsSource to the DataContext window and SelectedItem to the current ItemsSource DataGrid.

How can I do that?

Thanks!

+4
source share
6 answers

To avoid ElementName, where you should know NameScope well, you can also use FindAncestor. Here you determine the type of parent you want to bind to.

Example:

 <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Text}" /> 
+4
source

Use the ElementName binding expression. This will allow you to reference the window by name from the binding expression.

http://msdn.microsoft.com/en-us/library/system.windows.data.binding.elementname.aspx

The usage is well explained here.

http://www.wpfdude.com/articles/BindingToElement.aspx

+2
source
 <local:DataBaseSettings> <Grid> <DataGrid ItemsSource="{Binding Printers}"> <DataGrid.Columns> <DataGridComboBoxColumn Header="Drucker Typ" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:DataBaseSettings}}, Path=PrinterTypes}" SelectedItem="{Binding PrinterType, Mode=TwoWay}" /> </DataGrid.Columns> </DataGrid> </Grid> </local:DataBaseSettings> 

Note that if each Printer.PrinterType does not refer directly to an element that exists in your WindowContext Window, you need to overwrite the .Equals() method of your PrinterType class to make the two elements equal.

Sort of

 public override bool Equals(object obj) { if (obj == null) return false; if (this.GetType() != obj.GetType()) return false; PrinterType printerType= (PrinterType)obj; return (this.Id == printerType.Id); } 
0
source

until I found another way to do this than to set the itemssource at runtime as follows:

 private void SetComboBoxItemssource() { PrinterTypes = database.GetPrinterTypes(); DataGridComboBoxColumn cmbColumn = null; foreach (DataGridColumn column in dtaPrinters.Columns) { if ((cmbColumn = column as DataGridComboBoxColumn) != null) break; } if (cmbColumn == null) return; cmbColumn.ItemsSource = PrinterTypes; } 

it works though ..

0
source

I was able to get this working binding to the window resource:

 <DataGridComboboxColumn ItemsSource="{Binding Path=PrinterTypes, Source={StaticResource MyDataContext}}" ... /> 

I can't get it to work with RelativeSources, FindAncestors, etc.

0
source

The following worked for me. I just wanted to post it because the answer I used was baked in the comments of the fix_likes_codes answer. The key is the DataContext prefix before the property name in the path.

 <Button Command="{Binding Path=DataContext.SaveCommand, RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}}" CommandParameter="{Binding}"> 
0
source

All Articles