_Marcus_

I'm having problems with a scenario where I need to derive from DataTemplate, in order to add a property ( needed by my OM ).

The problem occurs when trying to use the derived class in XAML (works "fine" when creating the template in C# ).

Normally, using a DataTemplate in XAML would be (which works fine):

Code Snippet

<Button Content="foo">

<Button.ContentTemplate>

<DataTemplate>
<TextBlock Text="bar"/>
</DataTemplate>

</Button.ContentTemplate>

</Button>


Using the same syntax with my custom class:

Code Snippet

<Button Content="foo">

<Button.ContentTemplate>

<CustomDataTemplate>
<TextBlock Text="bar"/>
</CustomDataTemplate>

</Button.ContentTemplate>

</Button>


Gives me the following error:

'TextBlock' object cannot be added to 'CustomDataTemplate'. Object of type 'System.Windows.Controls.TextBlock' cannot be converted to type 'System.Windows.FrameworkElementFactory'.

Code for my CustomDataTemplate is:
Code Snippet

public class CustomDataTemplate : DataTemplate
{
public CustomDataTemplate( object dataType )
: base( dataType )
{
}


public bool IsFoo
{
get
{
return m_foo ;
}
set
{
m_foo = value;
}
}


private bool m_foo = false;

}



Anybody has an idea or suggestion




Re: Windows Presentation Foundation (WPF) Deriving from DataTemplate - Exception when used in XAML

_Marcus_

Just wanted to correct the class above... I forgot to paste the default constructor in the class definition of the original post... So the absence of the default constructor is not the reason for the error...







Re: Windows Presentation Foundation (WPF) Deriving from DataTemplate - Exception when used in XAML

lee d

I am not sure if you added the namespace mapping(say 'local') and then

you do

<local:CustomDataTemplate...>






Re: Windows Presentation Foundation (WPF) Deriving from DataTemplate - Exception when used in XAML

_Marcus_

I did not add the namespace mapping in the sample code from the forum post... but is present in my test application...

Anyway, the program would not compile at all if the namespace mapping was not present... In my case I receive a run-time XamlParseException with the error message posted above...

Sorry for yet another confusion!!! Wink

Any other ideas





Re: Windows Presentation Foundation (WPF) Deriving from DataTemplate - Exception when used in XAML

lee d

so your CustomDataTemplate in xaml looks something like

<local:CustomDataTemplate.../>






Re: Windows Presentation Foundation (WPF) Deriving from DataTemplate - Exception when used in XAML

_Marcus_

my XAML effectivelly looks like that (copy pasted from VS this time around)...

<Button Content="foo">

<Button.ContentTemplate>

<local:CustomDataTemplate>
<TextBlock Text="bar"/>
</local:CustomDataTemplate>

</Button.ContentTemplate>

</Button>


As I mentionned a bit earlier... the error occurs at run-time... I invite you to try it yourself, to give you an idea... The code provided here is well enough to reproduce the issue... (with the exceptions of the 2 small corrections caused by the fact I retyped the whole thing instead of pasting).









Re: Windows Presentation Foundation (WPF) Deriving from DataTemplate - Exception when used in XAML

lee d

I am not sure if that is the right way to do it.






Re: Windows Presentation Foundation (WPF) Deriving from DataTemplate - Exception when used in XAML

_Marcus_

I am not sure I understand the scope of your answer.

HierarchicalDataTemplate derives from DataTemplate and overrides nothing from the base class. HierarchicalDataTemplate still works...

Would you see an alternate way of doing it





Re: Windows Presentation Foundation (WPF) Deriving from DataTemplate - Exception when used in XAML

Douglas Stockwell

I've never seen anyone try to subclass a Template before. The thing I'd be worried about is that it's possible that the built-in DataTemplate types are special cased by the XAML parser. But certainly there's nothing in the documentation to that effect.





Re: Windows Presentation Foundation (WPF) Deriving from DataTemplate - Exception when used in XAML

_Marcus_

That's my biggest fear...

The fact that neither you or Lee have a clue about this is also a good pointer!! Wink

I would have liked to have confirmation of this from the MSFT team... Let's hope somebody from the team will take interest into this thread!.