Nal*_*are 4 wpf triggers background listbox controltemplate
我有一个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)
这似乎相当简单.我做了相同的基本过程ComboBox和TextBox成功.任何人都可以帮我看看我的代码错在哪里?如何正确地做到这一点的一个例子将是伟大的.上面列出的第一个例子似乎正是我所需要的,但正确答案是"这样做的唯一方法是覆盖模板",这对我没有帮助.
我已经尝试了几个简单的解决方案.一些其他风格可能会影响这一点,因为我们正在使用几个不同的资源字典.有人知道什么是追踪这个的好方法吗?
编辑: 我对整个解决方案进行了搜索,而ListBox正在被使用的唯一地方是我的部分,它唯一被设置样式的地方就是我设置的样式.根据MSDN,没有ListBox的"部分",所以我不可能在为其他控件设置样式的过程中无意中设置了ListBox的一部分.没有样式,当我禁用ListBox时,它被冻结但没有文本可见,并且具有默认背景.当我尝试应用Property ="Background"Value ="LightGray"时,它似乎被隐藏(即没有任何东西可见).如果有人知道为什么会这样做或如何完成我的任务,我会很感激帮助.
Bil*_*ell 11
sa_ddam213的答案对我不起作用所以我想我会添加我发现我必须做的事情.在我的情况下,我已设置透明背景,但当我禁用该框时,它将变为灰色.我希望能够在禁用控件时控制列表框的背景,这是我发现的工作方式.
注意:对于您的情况,您需要将透明颜色更改为您想要的任何灰度.
注意2:这可能仅在您没有更改列表框的模板时才有效.(更改datatemplate是好的).
<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
</ListBox.Style>
Run Code Online (Sandbox Code Playgroud)
小智 5
这两个答案都不适合我,所以我找到了一个覆盖适合我的 ControlTemplate 的解决方案:
<Style TargetType="{x:Type ListBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Grid Width="Auto" Height="Auto">
<Border x:Name="Border"
BorderThickness="1"/>
<ScrollViewer Focusable="false" IsTabStop="False" HorizontalScrollBarVisibility="Disabled">
<StackPanel IsItemsHost="true"/>
</ScrollViewer>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Border.Background" Value="{StaticResource DisabledBackgroundBrush}"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="{StaticResource DefaultBackground}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
我不认为你需要覆盖ControlTemplate,只需添加一个Style.Trigger对我来说效果很好的。
例子:
<ListBox>
<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="LightGray" />
<Setter Property="Background" Value="LightGray" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>
Run Code Online (Sandbox Code Playgroud)