venerdì 21 giugno 2013

[WPF] Sfondo di selezione trasparente ListBoxItem

Da incollare nel file App.xaml
<Style x:Key="ListBoxItemStyleTransparentSelect" TargetType="ListBoxItem">
 <Setter Property="Foreground" Value="Black"/>
 <Setter Property="FontSize" Value="10"/>
 <Setter Property="FontFamily" Value="Arial"/>
 <Setter Property="Padding" Value="1"/>
 <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
 <Setter Property="VerticalContentAlignment" Value="Top"/>
 <Setter Property="Background" Value="Transparent"/>
 <Setter Property="BorderThickness" Value="0"/>
 <Setter Property="Template">
  <Setter.Value>
   <ControlTemplate TargetType="ListBoxItem">
    <Grid Background="{TemplateBinding Background}">
     <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="CommonStates">
       <VisualState x:Name="Normal"/>
       <VisualState x:Name="MouseOver">
        <Storyboard>
         <DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/>
        </Storyboard>
       </VisualState>
       <VisualState x:Name="Disabled">
        <Storyboard>
         <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/>
        </Storyboard>
       </VisualState>
      </VisualStateGroup>
      <VisualStateGroup x:Name="SelectionStates">
       <VisualState x:Name="Unselected"/>
       <VisualState x:Name="Selected"/>
      </VisualStateGroup>
      <VisualStateGroup x:Name="FocusStates">
       <VisualState x:Name="Focused"/>
       <VisualState x:Name="Unfocused"/>
      </VisualStateGroup>
     </VisualStateManager.VisualStateGroups>
     <Rectangle x:Name="fillColor" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
     <Rectangle x:Name="fillColor2" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
     <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/>
     <Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" StrokeThickness="1" Visibility="Collapsed"/>
    </Grid>
   </ControlTemplate>
  </Setter.Value>
 </Setter>
</Style>

Nessun commento:

Posta un commento