Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change WPF Listbox/ListBoxItem DataTemplate for selected item WITHOUT affecting style & theming?
    primarykey
    data
    text
    <p>This question is very similar to <a href="https://stackoverflow.com/questions/146269/change-wpf-datatemplate-for-listbox-item-if-selected">Change WPF DataTemplate...</a>, which I have read and implemented. It worked beautifully at first, but I ran into a problem with it.</p> <p>That problem is that, when using themes in your application such as those in the <a href="http://wpf.codeplex.com/wikipage?title=WPF%20Themes&amp;referringTitle=Home" rel="nofollow noreferrer">WPF Futures project</a> (e.g. Expression Dark), the ListBoxItems all revert back to the default WPF styling. This breaks the theme for those elements and, for example, produces black text on black background where the text would otherwise be white. This also affected my TreeView, and presumably would affect other similar controls.</p> <p>I think this is because conflicting styles are being set for ListBox.ItemContainerStyle--one from the theme and one for switching data templates.</p> <p>I've looked around for other solutions, but haven't found anything yet. Here are the leads or ideas I've had so far:</p> <ol> <li>Subclassing <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx" rel="nofollow noreferrer">DataTemplateSelector</a> and setting it to ListBox.ItemTemplateSelector. (The current best bet).</li> <li>Somehow, somewhere use a Trigger, DataTrigger, or EventTrigger.</li> <li>Give up on themes.</li> <li>Somehow hack the functionality I want into the theme.</li> <li>Somehow make my custom ItemContainerStyle somehow inherit it's colors and eye candy from the theme's style. (I tried it briefly, and it didn't work.)</li> </ol> <p>Here is my ListBox and related pieces:</p> <pre><code>&lt;Window.Resources&gt; &lt;DataTemplate x:Key="NormalTemplate"&gt; ... &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="SelectedTemplate"&gt; ... &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;ListBox x:Name="RegisterListBox" Grid.Row="0" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Adjustments}"&gt; &lt;!-- this is from the post referenced above --&gt; &lt;ListBox.ItemContainerStyle&gt; &lt;Style TargetType="{x:Type ListBoxItem}"&gt; &lt;Setter Property="ContentTemplate" Value="{StaticResource NormalTemplate}"/&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsSelected" Value="True"&gt; &lt;Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/ListBox.ItemContainerStyle&gt; &lt;/ListBox&gt; </code></pre> <p>The Listbox.DataContext is set in code to enable the ItemsSource binding.</p> <p>Any ideas how I can achieve the kind of functionality described above while maintaining seamless support for themes?</p>
    singulars
    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