Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datatemplate in app.xaml is not getting picked up without any styles?

I have a DataTemplate in app.xaml that binds a view to a viewmodel.

<Application.Resources>
    <DataTemplate DataType="{x:Type vm:someviewmodeltype}">
        <vw:somevwcontrol />
    </DataTemplate>
</Application.Resources>

the above template doesn't get applied if there are no styles. The moment I put a style, something like ...

<Application.Resources>
    <DataTemplate DataType="{x:Type vm:someviewmodeltype}">
        <vw:somevwcontrol />
    </DataTemplate>
    <Style TargetType="TextBlock">
        <Setter Property="FontSize" Value="20"></Setter>
    </Style>
</Application.Resources>

datatemplate gets picked up and produces the desired results ... I am not sure whats happening there ... could anybody clarify this ?

Thanks.

like image 502
Ramki Avatar asked Jan 27 '11 06:01

Ramki


People also ask

What is DataTemplate in XAML?

Data template is a bit of XAML that describes how bound data is displayed. A data template can contain elements that are each bound to a data property along with additional markup that describes layout, color and other appearance.

What is DataTemplate in WPF?

DataTemplate is about the presentation of data and is one of the many features provided by the WPF styling and templating model. For an introduction of the WPF styling and templating model, such as how to use a Style to set properties on controls, see the Styling and Templating topic.

What is a data template?

The data template is the method by which you communicate your request for data to the data engine. It is an XML document whose elements collectively define how the data engine will process the template to generate the XML. The data engine supports the following functionality: Single and multiple data queries.


2 Answers

Answered a similar question here. The question is not exactly the same, that one contained merged dictionaries being skipped but it's most likely the same bug.

This is an optimization bug, see this link.

On the creation of every object in XAML, if a default style is present (i.e. style w/ a key of Type) that style should be applied. As you can imagine there are several performance optimizations to make that (implied) lookup a light weight as possible. One of them is that we don’t look inside Resource Dictionaries unless they are flagged as “containing default Styles”. There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it. The work around is to put a default Style to something, anything, in the root Dictionary.

I see you've already found the workaround as well, just add a default dummy style in App.xaml. It doesn't have to have any setters etc, something like this will do as well

<Application.Resources>     <DataTemplate DataType="{x:Type vm:someviewmodeltype}">         <vw:somevwcontrol />     </DataTemplate>     <Style TargetType="{x:Type Rectangle}" />  </Application.Resources> 
like image 162
Fredrik Hedblad Avatar answered Sep 19 '22 15:09

Fredrik Hedblad


Another pitfall is leaving out the {x:Type} from the DataType attribute.

WRONG

This builds, runs and fails silently:

<DataTemplate DataType="local:MyType">

RIGHT

<DataTemplate DataType="{x:Type local:MyType}">
like image 31
Drew Noakes Avatar answered Sep 23 '22 15:09

Drew Noakes