如何更改所选项目的WPF Listbox/ListBoxItem DataTemplate而不影响样式和主题?

Ben*_*gan 1 data-binding wpf xaml templates themes

这个问题非常类似于我已经阅读并实现的Change WPF DataTemplate .... 它起初很漂亮,但我遇到了问题.

问题在于,当您的应用程序中使用主题(例如WPF Futures项目中的主题)(例如Expression Dark)时,ListBoxItems都会恢复为默认的WPF样式.这打破了这些元素的主题,例如,在黑色背景上生成黑色文本,否则文本将为白色.这也影响了我的TreeView,可能会影响其他类似的控件.

我认为这是因为为ListBox.ItemContainerStyle设置了冲突的样式 - 一个来自主题,一个来自切换数据模板.

我四处寻找其他解决方案,但还没有找到任何解决方案.以下是我到目前为止的主要内容或想法:

  1. DataTemplateSelector进行子类化并将其设置为ListBox.ItemTemplateSelector.(目前最好的选择).
  2. 不知何故,某处使用Trigger,DataTrigger或EventTrigger.
  3. 放弃主题.
  4. 以某种方式破解我想要的主题功能.
  5. 不知何故,我的自定义ItemContainerStyle以某种方式从主题的风格继承了它的颜色和眼睛糖果.(我试了一下,但是没用.)

这是我的ListBox和相关部分:

<Window.Resources>

  <DataTemplate x:Key="NormalTemplate">
      ...
  </DataTemplate>

  <DataTemplate x:Key="SelectedTemplate">
      ...
  </DataTemplate>

</Window.Resources>

<ListBox x:Name="RegisterListBox" Grid.Row="0"
         HorizontalContentAlignment="Stretch"
         ItemsSource="{Binding Adjustments}">

    <!-- this is from the post referenced above -->
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="ContentTemplate" Value="{StaticResource NormalTemplate}"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

Listbox.DataContext在代码中设置以启用ItemsSource绑定.

任何想法如何在保持对主题的无缝支持的同时实现上述功能?

arc*_*aut 5

你有没有试过这样的事情?

<ListBox.ItemContainerStyle>
    <Style 
        TargetType="{x:Type ListBoxItem}" 
        BasedOn="{StaticResource {x:Type ListBoxItem}}">    <=====
...
Run Code Online (Sandbox Code Playgroud)

我们的想法是,框架将首先寻找具有等于键的样式,typeof(ListBoxItem)它将在主题中找到它,然后您的样式将仅使用您的特定细节扩展主题.