You can disable visual effects of the selection by using this ListBox:
<Style x:Key="NoSelectionListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style>
<!-- This removes focus visualization -->
<Setter Property="Control.Template" Value="{x:Null}"/>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
<!-- Some default triggers removed to avoid background changes on selection -->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Perhaps a cleaner solution would be to create your own ItemsControl with specific item containers that could have their style.
dynamically generated listbox items
sounds really odd and too winforms. you should use DataBinding, and yes, as @Didier said you need anItemsControl
instead of aListBox
. – Federico Berasategui<Style TargetType="{x:Type ListBoxItem}">
XAML section I was using to control borders, highlighting, etc. on individual listbox items upon mouseover events. Using ItemsControl, the styling gets applied to the entire list, not just a singluar item. I tried re-writing my styling using<Style TargetType="{x:Type ItemsControlItem}">
but that doesn't exist. Options? – user3342256