相关疑难解决方法(0)

将禁用的列表框背景更改为灰色

我有一个ListBox我需要在它被禁用时变灰的东西.根据用户请求,禁用它是不够的,但它也必须以不同的方式显示.耸耸肩 我看了几个其他的地方,并按照例子,似乎它应该工作,但事实并非如此.下面是一些例子我看了看:例1,例2.

这是我的代码:

<Style x:Key="ListBoxStyle" TargetType="ListBox">
 <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="BorderBrush" Value="Black"></Setter>
                        <Setter Property="Foreground" Value="LightGray"></Setter>
                            <Setter Property="Background" Value="LightGray"></Setter>
                        <Setter Property="BorderBrush" Value="LightGray"></Setter>
                    </Trigger>
                 </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>                 
 </Style>
Run Code Online (Sandbox Code Playgroud)

这似乎相当简单.我做了相同的基本过程ComboBoxTextBox成功.任何人都可以帮我看看我的代码错在哪里?如何正确地做到这一点的一个例子将是伟大的.上面列出的第一个例子似乎正是我所需要的,但正确答案是"这样做的唯一方法是覆盖模板",这对我没有帮助.

我已经尝试了几个简单的解决方案.一些其他风格可能会影响这一点,因为我们正在使用几个不同的资源字典.有人知道什么是追踪这个的好方法吗?

编辑: 我对整个解决方案进行了搜索,而ListBox正在被使用的唯一地方是我的部分,它唯一被设置样式的地方就是我设置的样式.根据MSDN,没有ListBox的"部分",所以我不可能在为其他控件设置样式的过程中无意中设置了ListBox的一部分.没有样式,当我禁用ListBox时,它被冻结但没有文本可见,并且具有默认背景.当我尝试应用Property ="Background"Value ="LightGray"时,它似乎被隐藏(即没有任何东西可见).如果有人知道为什么会这样做或如何完成我的任务,我会很感激帮助.

wpf triggers background listbox controltemplate

4
推荐指数
3
解决办法
6934
查看次数

ListBox选择的项目背景

我正在尝试更改WPF ListBox中所选项目的背景.

我试图为它实现一种风格,但由于某种原因它没有被应用.我仍然得到一个蓝色的背景.谁能明白为什么?

<UserControl x:Class="Thumbnails"
         xmlns:local="clr-namespace:ContentPresenter"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="350" d:DesignWidth="800">
<UserControl.Resources>
    <local:ThumbImageHeightConverter x:Key="HeightConv" />
    <local:ThumbImageWidthConverter x:Key="WidthConv" />
    <local:InnerGridHeightConverter x:Key="InnerGridHeightConv" />
    <local:ReflectWidthConverter x:Key="ReflectWidthConv" />
    <local:ReflectCenterYConv x:Key="ReflectCenterYConv" />

    <Style x:Name="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <Trigger Property="Selector.IsSelected" Value="True">
                <Setter Property="Background" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style TargetType="{x:Type ListBox}">
        <!-- Set the ItemTemplate of the ListBox to a DataTemplate which explains how to display an object of type BitmapImage. -->
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid x:Name="ThumbGrid" VerticalAlignment="Top" Height="{Binding ElementName=ThumbListBox, Path=ActualHeight}"  >
                        <Grid.RowDefinitions> …
Run Code Online (Sandbox Code Playgroud)

wpf xaml templates listbox selecteditem

1
推荐指数
2
解决办法
1万
查看次数