.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Can I override the disabled background color for a listbox?
 

Can I override the disabled background color for a listbox?

I have a listbox that's for display only purposes. In fact, the item template is a WrapPanel. I want to override the background color to be the same color as the grid it's in.

Setting the background color has no effect, as the base style overrides it. I thought I might be able to locally define and override a system brush in the the listbox, but that got me nowhere (not even sure which system color is being used... if that's even on the right track in the first place).

Does anyone know how to do this?

Thank you.

Todd Beaulieu
Try putting it in your Window or UserControl.

Code Snippet

<UserControl.Resources>

<Style TargetType="{x:Type ListBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Grid Width="Auto" Height="Auto">
<Border x:Name="Border" CornerRadius="0,0,0,0"/>
<ScrollViewer Focusable="false" IsTabStop="False" HorizontalScrollBarVisibility="Disabled">
<StackPanel IsItemsHost="true"/>
</ScrollViewer>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" Value="Yellow"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0,0,0,0"/>
<Setter Property="Background" Value="White"/>
</Style>
</UserControl.Resources>


I copied this from one of my ListBoxes, maybe it will work for you. This will set the default style for all ListBoxes in the control, otherwise set x:Key on this Style and set the style of your ListBox to {StaticResource YourKey} use it on a specific ListBox.
Let me know if this works.
Jeff Wain
What you can do is create a new style for a ListBox in your resources (or in a ResourceDictionary). Then you can set a trigger on the style to check when IsEnabled is false and then set the style for the ListBox then. If you need an example of how to do this, just let me know.
Jeff Wain

I actually tried creating a style, but couldn't get it to work. Is this what you mean?

<Grid.Resources>

<Style TargetType="ListBox">

<Style.Triggers>

<Trigger Property="Control.IsEnabled" Value="False">

<Setter Property="Background" Value="Yellow" />

</Trigger>

</Style.Triggers>

</Style>

</Grid.Resources>

I have this in the Grid that houses the ListBox. Actually, I must be doing something wrong, because I can't seem to get this to set anything. I added a True trigger that sets the BorderThickness and changed my code to enable the list. I don't even get that change to show up. If I hard-code the value, I can clearly see it.

Can you spot anything wrong with this logic? Thank you.

Todd Beaulieu
Try putting it in your Window or UserControl.

Code Snippet

<UserControl.Resources>

<Style TargetType="{x:Type ListBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Grid Width="Auto" Height="Auto">
<Border x:Name="Border" CornerRadius="0,0,0,0"/>
<ScrollViewer Focusable="false" IsTabStop="False" HorizontalScrollBarVisibility="Disabled">
<StackPanel IsItemsHost="true"/>
</ScrollViewer>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" Value="Yellow"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0,0,0,0"/>
<Setter Property="Background" Value="White"/>
</Style>
</UserControl.Resources>


I copied this from one of my ListBoxes, maybe it will work for you. This will set the default style for all ListBoxes in the control, otherwise set x:Key on this Style and set the style of your ListBox to {StaticResource YourKey} use it on a specific ListBox.
Let me know if this works.
Jeff Wain

You can use google to search for other answers

Custom Search

More Threads

• How to format numbers in XAML?
• SetResourceReference in Tab Item
• How to reference one xaml file from another?
• Listview Sort does not work more than once for me
• How to send messages from a WPF app to a WinFor?
• XBAP cancelled by user message ... after closing a DHTML popup
• XAML specification
• Drag and Drop in the Same TreeView
• Problem with dynamically created tooltips
• How to bind a ObservableCollection<T> to ListBox Items with synchronized update?