Wpf No Highlight on ListBoxItem for Touch Devices

169 views Asked by At

I have defined my ListBox to not show any highlight on the ListBoxItem as follows:

<ListBox ItemsSource="{Binding MyCollection}"
         Style="{x:Null}"
         ScrollViewer.CanContentScroll="False">

        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                 <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
                </Style.Resources>
            </Style>
        </ListBox.ItemContainerStyle>

        <ItemsControl.ItemTemplate>
            <DataTemplate>

                <Button Content="{Binding SomeBinding}"/>

            </DataTemplate>
        </ItemsControl.ItemTemplate>

    </ListBox>

Whilst this works on a desktop, as soon as I try running it on a touch device (e.g Microsoft Surface) I still get the default blue highlight on hover and selection. Any ideas what I'm missing for touch devices?

Update

Appears the easiest solution is to simply overwrite the style for the ListBoxItem, i.e.,

<Style TargetType="ListBoxItem" x:Key="NonSelectingListBoxItemStyle">
    <Setter Property="Template">
        <Setter.Value>
             <ControlTemplate TargetType="ListBoxItem">
                  <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                  </ControlTemplate>
             </Setter.Value>
     </Setter>
</Style>

although still not sure why setting the brushes as in the first section of xaml code above does not work on touch devices.

1

There are 1 answers

1
ReeganLourduraj On

shall remove solidcolorbrush key values and give as ordinary key value.system colors may hold default colors and it produces blue highlight.

change your this part of code

     <SolidColorBrush x:Key="Background" Color="Transparent"/>
 <SolidColorBrush x:Key="BorderBrush" Color="Transparent"/>