更改组合框项目WPF的突出显示颜色

时间:2017-07-09 14:27:17

标签: wpf combobox highlighting listboxitems

我一直在寻找如何改变组合框项目的高亮颜色( NOT MouseOver )。我有一个可编辑的组合框,用于搜索列表中绑定到它的元素。在列表中键入元素名称将突出显示组合框下拉列表中的元素。但该元素在默认情况下突出显示"浅灰色"很难看到...

enter image description here

我已尝试覆盖本文中描述的默认突出显示颜色:Set ComboBox selected item highlight color但没有运气。

在盒子中搜索或使用键盘上的向上/向下箭头键导航列表时,不会发生这种情况。我也考虑过可能会关注键盘焦点属性但不确定如何在模板中实现它。让我知道,如果需要,我可以发布模板。

我刚为这个组合框启动了一个全新的模板,所以它应该都非常干净。我无法确定我应该定位的元素/触发器/边框。

有人对此有任何见解吗?

1 个答案:

答案 0 :(得分:0)

通过在此处处理本文找到答案:Set style on ComboBoxItem from template for ComboBox

我将一个修改过的ComboBoxItem模板应用到我的" Editable ComboBox"模板。你可以在这里看到我添加了一个" ItemContainerStyle" IsEditable 样式触发器中的属性:

Collecting django-misaka
  Downloading django-misaka-0.2.1.tar.gz
Collecting houdini.py (from django-misaka)
  Downloading houdini.py-0.1.0.tar.gz
Collecting misaka (from django-misaka)
  Downloading misaka-2.1.0.tar.gz (127kB)
    100% |████████████████████████████████| 133kB 317kB/s 
    Complete output from command python setup.py egg_info:
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libffi' found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libffi' found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libffi' found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libffi' found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libffi' found
    gcc: error: /usr/lib/rpm/redhat/redhat-hardened-cc1: No such file or directory
    gcc: error: /usr/lib/rpm/redhat/redhat-hardened-cc1: No such file or directory

        No working compiler found, or bogus compiler options
        passed to the compiler from Python's distutils module.
        See the error messages above.
        (If they are about -mno-fused-madd and you are on OS/X 10.8,
        see http://stackoverflow.com/questions/22313407/ .)
    Traceback (most recent call last):
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 156, in save_modules
        yield saved
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 197, in setup_context
        yield
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 246, in run_setup
        DirectorySandbox(setup_dir).run(runner)
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 276, in run
        return func()
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 245, in runner
        _execfile(setup_script, ns)
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 47, in _execfile
        exec(code, globals, locals)
      File "/tmp/easy_install-j_v6nkgj/cffi-1.10.0/setup.py", line 135, in <module>
      File "/tmp/easy_install-j_v6nkgj/cffi-1.10.0/setup.py", line 70, in ask_supports_thread
        'Topic :: Text Processing :: Markup',
      File "/tmp/easy_install-j_v6nkgj/cffi-1.10.0/setup.py", line 52, in no_working_compiler_found
        packages=['misaka'],
    SystemExit: 1

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/usr/lib/python3.5/site-packages/setuptools/command/easy_install.py", line 1099, in run_setup
        run_setup(setup_script, args)
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 249, in run_setup
        raise
      File "/usr/lib64/python3.5/contextlib.py", line 77, in __exit__
        self.gen.throw(type, value, traceback)
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 197, in setup_context
        yield
      File "/usr/lib64/python3.5/contextlib.py", line 77, in __exit__
        self.gen.throw(type, value, traceback)
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 168, in save_modules
        saved_exc.resume()
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 143, in resume
        six.reraise(type, exc, self._tb)
      File "/usr/lib/python3.5/site-packages/pkg_resources/_vendor/six.py", line 685, in reraise
        raise value.with_traceback(tb)
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 156, in save_modules
        yield saved
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 197, in setup_context
        yield
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 246, in run_setup
        DirectorySandbox(setup_dir).run(runner)
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 276, in run
        return func()
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 245, in runner
        _execfile(setup_script, ns)
      File "/usr/lib/python3.5/site-packages/setuptools/sandbox.py", line 47, in _execfile
        exec(code, globals, locals)
      File "/tmp/easy_install-j_v6nkgj/cffi-1.10.0/setup.py", line 135, in <module>
      File "/tmp/easy_install-j_v6nkgj/cffi-1.10.0/setup.py", line 70, in ask_supports_thread
        'Topic :: Text Processing :: Markup',
      File "/tmp/easy_install-j_v6nkgj/cffi-1.10.0/setup.py", line 52, in no_working_compiler_found
        packages=['misaka'],
    SystemExit: 1

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-fkyx6m64/misaka/setup.py", line 76, in <module>
        cffi_modules=['build_ffi.py:ffi'],
      File "/usr/lib64/python3.5/distutils/core.py", line 108, in setup
        _setup_distribution = dist = klass(attrs)
      File "/usr/lib/python3.5/site-packages/setuptools/dist.py", line 348, in __init__
        self.fetch_build_eggs(attrs['setup_requires'])
      File "/usr/lib/python3.5/site-packages/setuptools/dist.py", line 394, in fetch_build_eggs
        replace_conflicting=True,
      File "/usr/lib/python3.5/site-packages/pkg_resources/__init__.py", line 826, in resolve
        dist = best[req.key] = env.best_match(req, ws, installer)
      File "/usr/lib/python3.5/site-packages/pkg_resources/__init__.py", line 1098, in best_match
        return self.obtain(req, installer)
      File "/usr/lib/python3.5/site-packages/pkg_resources/__init__.py", line 1110, in obtain
        return installer(requirement)
      File "/usr/lib/python3.5/site-packages/setuptools/dist.py", line 461, in fetch_build_egg
        return cmd.easy_install(req)
      File "/usr/lib/python3.5/site-packages/setuptools/command/easy_install.py", line 663, in easy_install
        return self.install_item(spec, dist.location, tmpdir, deps)
      File "/usr/lib/python3.5/site-packages/setuptools/command/easy_install.py", line 693, in install_item
        dists = self.install_eggs(spec, download, tmpdir)
      File "/usr/lib/python3.5/site-packages/setuptools/command/easy_install.py", line 874, in install_eggs
        return self.build_and_install(setup_script, setup_base)
      File "/usr/lib/python3.5/site-packages/setuptools/command/easy_install.py", line 1113, in build_and_install
        self.run_setup(setup_script, setup_base, args)
      File "/usr/lib/python3.5/site-packages/setuptools/command/easy_install.py", line 1101, in run_setup
        raise DistutilsError("Setup script exited with %s" % (v.args[0],))
    distutils.errors.DistutilsError: Setup script exited with 1

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-fkyx6m64/misaka/

MultiTrigger是自定义&#34; ItemContainerStyle&#34;的一部分。 ControlTemplate(重要的是要注意,这是一个Style而不是一个实际的ControlTemplate。)。 ControlTemplate元素位于样式中。

您可以生成&#34; ComboboxItem&#34;模板通过手动将ComboBoxItem添加到XAML中的现有ComboBox并从中生成模板。

<Style x:Key="CmbRoundedAutoComplete" TargetType="{x:Type ComboBox}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource ComboBox.Static.Background}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ComboBox.Static.Border}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="Padding" Value="6,3,5,3"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
        <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
        <Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"/>
        <Style.Triggers>
            <Trigger Property="IsEditable" Value="true">
                <Setter Property="IsTabStop" Value="false"/>
                <Setter Property="Padding" Value="2"/>
                <Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}"/>
               <!-- Added this line here --> 
                <Setter Property="ItemContainerStyle" Value="{DynamicResource ComboBoxItemShieldStyle}" />
            </Trigger>
        </Style.Triggers>
    </Style>

您可以在上面的代码中看到我评论的内容&#34;答案在这里&#34;您可以通过键入组合框字段或使用箭头键导航到所选项目的背景颜色来改变所选项目的背景颜色。

您可以在&#34;结果&#34; (以红色文字表示)&#34;背景&#34;当前项目的颜色是漂亮的蓝色,而不是上面原始问题中的蹩脚灰色......

最终结果:

enter image description here