0

In a DataTemplate I want to show the Control which is actually replaced by the DataTemplate. With error validation, this is possible by using <AdornedElementPlaceHolder />. However, this seems not to work in a normal DataTemplate.

I think there would be a simple solution, but just can't find it.

Thanks a lot

Peter van Kekem
  • 1,387
  • 1
  • 12
  • 30

2 Answers2

1

This is not possible and here is why. DataTemplate doesn't replace anything. Instead, a control that defines a DataTemplate contains the content provided by it. If it was possible then there would be an infinite loop (control -> data template -> control -> data template -> ...).

I suggest you read the following article to fully understand data templates: http://msdn.microsoft.com/en-us/library/ms742521.aspx

Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
1

A DataTemplate in some sense "replaces" a data object and not a UI object - in other words, not a Control. If you are trying to work with properties of the Control (usually either a ContentControl or ItemsControl) that is using the template, try a RelativeSource Binding where the AncestorType is the type of the Control.

Community
  • 1
  • 1
Dana Cartwright
  • 1,566
  • 17
  • 26
  • What about the ControlTemplate? – Peter van Kekem Feb 17 '11 at 07:33
  • If you set the control template, you are completely replacing the original "look" of the control. I don't know of any way to reference the original template inside of a new one, as it's pretty much a wholesale replacement. Generally the approach people take is to use Expression Blend to "bake out" the standard control template and then modify the fully expanded one to suit their needs. It's clunky, but it works. – Dana Cartwright Feb 17 '11 at 15:10
  • Okay, I'll try that. In the error validation template, you can get the original control by ''. – Peter van Kekem Feb 18 '11 at 07:49
  • That's because error validation is using an adorner on top of the existing element. Both data templates and control templates are actually modifying the visual tree itself instead of tacking something in "on top" of the existing visuals. – Dana Cartwright Feb 18 '11 at 12:55