如何更改选定项目的WPF Listbox / ListBoxItem DataTemplate,而不影响样式&主题? [英] How to change WPF Listbox/ListBoxItem DataTemplate for selected item WITHOUT affecting style & theming?

查看:152
本文介绍了如何更改选定项目的WPF Listbox / ListBoxItem DataTemplate,而不影响样式&主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与更改WPF DataTemplate ... ,我已经阅读并实施了。它首先工作得很好,但是我遇到了一个问题。

This question is very similar to Change WPF DataTemplate..., which I have read and implemented. It worked beautifully at first, but I ran into a problem with it.

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

That problem is that, when using themes in your application such as those in the WPF Futures project (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.

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

I think this is because conflicting styles are being set for ListBox.ItemContainerStyle--one from the theme and one for switching data templates.

我已经看了其他解决方案,但还没有找到任何东西。以下是我到目前为止的线索或想法:

I've looked around for other solutions, but haven't found anything yet. Here are the leads or ideas I've had so far:


  1. 子类化 DataTemplateSelector 并将其设置为ListBox.ItemTemplateSelector。 (目前最好的选择)。

  2. 不知何故,某处使用Trigger,DataTrigger或EventTrigger。

  3. 放弃主题。
  4. 以某种方式破解了我想要的主题的功能。

  5. 以某种方式使我的自定义ItemContainerStyle从主题的风格继承了它的颜色和眼睛糖果。 (我试过一下,它没有工作。)

  1. Subclassing DataTemplateSelector and setting it to ListBox.ItemTemplateSelector. (The current best bet).
  2. Somehow, somewhere use a Trigger, DataTrigger, or EventTrigger.
  3. Give up on themes.
  4. Somehow hack the functionality I want into the theme.
  5. 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.)

这是我的ListBox和相关的部分:

Here is my ListBox and related pieces:

<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>

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

The Listbox.DataContext is set in code to enable the ItemsSource binding.

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

Any ideas how I can achieve the kind of functionality described above while maintaining seamless support for themes?

推荐答案

p>你是否尝试过这样的事情?

Have you tried doing something like this?

<ListBox.ItemContainerStyle>
    <Style 
        TargetType="{x:Type ListBoxItem}" 
        BasedOn="{StaticResource {x:Type ListBoxItem}}">    <=====
...

想法是框架将首先去寻找一个等于 typeof(ListBoxItem)的键的样式,它会在主题中找到它,然后你的风格只会用你的具体的细节。

The idea is that the framework will first go look for a style with a key equal to typeof(ListBoxItem), it will find it in the themes, and then your style will just extend the themed one with your specific details.

这篇关于如何更改选定项目的WPF Listbox / ListBoxItem DataTemplate,而不影响样式&amp;主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆