Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can data templates in generic.xaml get applied automatically (redux)?
    primarykey
    data
    text
    <p>This is a follow-up to a <a href="https://stackoverflow.com/questions/2751707/how-can-data-templates-in-generic-xaml-get-applied-automatically">question</a> posted by <a href="https://stackoverflow.com/users/304141/thiado-de-arruda">Thiado de Arruda</a>. In short, he wanted to have a DataTemplate in his generic.xaml file, but the template wasn't being applied.</p> <p>The answer given suggested placing the DataTemplate in the ControlTemplate.Resources for the control that hosted his custom type. This works very well, however, suppose that he needed the DataTemplate to apply in other places, not just within the host control. Would it be necessary to copy the DataTemplate to the ControlTemplates of every other host control?</p> <p><strong>Edit</strong> (restating question):</p> <p>I am developing a WPF application using MVVM design principles. <em>MainWindow.xaml</em> contains the structure of the UI, and all of the styling is coded in <em>Themes\generic.xaml</em>. (The behavior is coded in a separate view model class, but that's irrelevant.) As part of the UI, I created a subclass of <code>ListBox</code> (<code>MyListBoxSubClass</code>) to display a collection of an ordinary .Net object of my own creation (<code>MyObject</code>). <code>MyListBoxSubClass</code> has a style in <em>generic.xaml</em> that redefines the <code>Template</code> property, and it gets applied as expected. I also have a <code>DataTemplate</code> for <code>MyObject</code> in <em>generic.xaml</em>, but this does not get applied. According to the above link, I have to place the <code>DataTemplate</code> in the <code>Resources</code> collection of the <code>ControlTemplate</code> for <code>MyListBoxSubClass</code> in order for this <code>DataTemplate</code> to be applied. This works wonderfully.</p> <p>My question is how to get the <code>DataTemplate</code> to apply to <code>MyObject</code> <em>everywhere</em> in my application without having to duplicate the <code>DataTemplate</code>? I've tried adding a key to the <code>DataTemplate</code> and referencing it where I need it, but for some reason, I get a XAML parse error at runtime, and Resharper says that it can't resolve my <code>DataTemplate</code> key.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload