0

I want to disable the visual effect on Mouse Over on each item of my listbox, and I would like to disable the visual effect when a user clicks, too.

I read that it could be done using a DataTrigger on Windows Phone, but on Windows 8, we can not use DataTrigger : DataTrigger in WinRT?

What else can I use to delete this visual effect ?
I saw the StyleSelector/ListViewItemStyleSelector, can I use this?
If yes, where can I find a sample, because i did not understood how it work.

Community
  • 1
  • 1
NicoMinsk
  • 1,716
  • 6
  • 28
  • 53

2 Answers2

3

If you mean disable the selected item style, then in WPF you can do this:

<Style x:Key="NullSelectionStyle" TargetType="ListBoxItem">
    <Style.Resources>
        <!-- SelectedItem with focus -->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <!-- SelectedItem without focus -->
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
        <!-- SelectedItem text foreground -->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{DynamicResource {x:Static SystemColors.ControlTextColorKey}}" />
    </Style.Resources>
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>

<ListBox ItemContainerStyle="{StaticResource NullSelectionStyle}" ...>

Unfortunately I don't have access to Windows 8 yet, so I can't say if it works on WinRT.

Alternatively if you don't need any selection at all, just use an ItemsControl.

For example, instead of <ListBox .../> use <ItemsControl .../>. An ItemsControl shows a list of items like a ListBox but has no concept of the selected item.

Phil
  • 42,255
  • 9
  • 100
  • 100
1

If you want to edit the ListBox template for a Metro Style app, you can remove the animations from the MouseOver VisualState. Here is a ListBoxItem template that would work.

<Style x:Key="NoSelectListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Disabled">
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ContentContainer">
                                                <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Selected"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{StaticResource Dark_Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" d:LayoutOverrides="Width"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

And apply the style

<ListBox ItemContainerStyle="{StaticResource NoSelectListBoxItemStyle}" />
kindasimple
  • 2,427
  • 1
  • 16
  • 20