2009-05-08 10 views
1

J'ai trouvé ce code qui remplace le style de sélection par défaut par un style personnalisé pour le ListBoxItem sélectionné dans un contrôle ListBox. Cependant, il y a toujours une petite ligne bleue à gauche du style par défaut que je ne peux pas enlever avec des changements de marge ou de marge.Comment supprimer la ligne bleue de gauche sur les éléments Listbox personnalisés et personnalisés?

Comment puis-je supprimer cette ligne bleue et déterminer complètement le style du ListBoxItem sélectionné?

alt text http://tanguay.info/web/external/blueLineLeft.png

<Window x:Class="CodingContext.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:CodingContext" 
    Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 

     <DataTemplate x:Key="ItemTemplate" DataType="{x:Type local:Person}"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
        <ColumnDefinition Width="Auto" /> 
       </Grid.ColumnDefinitions> 

       <TextBlock Grid.Column="0" Text="{Binding Path=Name, StringFormat=Name: \{0\}}" /> 
       <TextBlock Grid.Column="1" Text="{Binding Path=Age, StringFormat=Age: \{0\}}" /> 
      </Grid> 
     </DataTemplate> 

     <DataTemplate x:Key="SelectedTemplate" DataType="{x:Type local:Person}"> 
      <Grid Background="Yellow"> 

       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
        <ColumnDefinition Width="Auto" /> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
        <RowDefinition /> 
        <RowDefinition /> 
        <RowDefinition /> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 

       <TextBlock Foreground="Black" Grid.Row="0" Grid.Column="0" Text="Person" FontSize="14" FontWeight="Bold" /> 
       <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Path=Name, StringFormat=Name: \{0\}}" /> 
       <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Age, StringFormat=Age: \{0\}}" /> 

       <TextBlock Grid.Row="2" Grid.Column="0" Text="Address" FontSize="14" FontWeight="Bold" /> 
       <StackPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal"> 
        <TextBlock Text="{Binding Address}" /> 
        <TextBlock Text="{Binding City, StringFormat= \{0\}}" /> 
        <TextBlock Text="{Binding State, StringFormat= \{0\}}" /> 
        <TextBlock Text="{Binding Zip, StringFormat= \{0\}}" /> 
       </StackPanel> 
      </Grid> 
     </DataTemplate> 

     <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle"> 
      <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" /> 
      <Setter Property="Margin" Value="5" /> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 

    </Window.Resources> 

    <ListBox HorizontalContentAlignment="Stretch" Margin="10" x:Name="lstPeople" ItemsSource="{Binding People}" ItemContainerStyle="{StaticResource ContainerStyle}" /> 

</Window> 

Répondre

4

Je suis assez sûr que c'est un bug, car plusieurs personnes ont eu ce problème avec le modèle ListBoxItem.

Pour fixer, il suffit d'ajouter:

<Setter Property="Padding" Value="0,0,0,0"/> 

à votre modèle ContainerStyle. C'est à l'origine 2,0,0,0.

+0

oui ça marche, il faut le tripoter pour le rembourrer (j'ai dû appliquer une marge à chacun des blocs de texte enfant, une bordure avec un rembourrage qui ramène la ligne bleue) mais c'est faisable, merci . –

1

Jetez un oeil à this answer to a similar question. Fondamentalement, si vous souhaitez redimensionner la ListBox afin que les éléments sélectionnés aient une couleur d'arrière-plan différente, il est plus facile de simplement changer la couleur "HighlightBrush" de sorte que l'élément sélectionné utilise la couleur que vous voulez.

Questions connexes