2009-05-08 8 views
1

このコードは、デフォルトの選択スタイルをListBoxのSelect ListBoxItemのカスタムスタイルに置き換えています。ただし、パディングや余白の変更では削除できない既定のスタイルから、左に少し青い線が残ります。選択したカスタムスタイルのリストボックスアイテムの左側の青い線を削除するにはどうすればよいですか?

青い線を削除して、選択したListBoxItemのスタイルを完全に決定するにはどうすればよいですか?

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> 

答えて

4

多くの人がListBoxItemテンプレートでこの問題を抱えているので、これはバグだと確信しています。

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

あなたContainerStyleテンプレートに:ちょうど追加、修正するには

。元は2,0,0,0です。

+0

はい、それを元に戻ってパディングを取得する必要があります(私はそれぞれの子のtextblocksに余白を適用しなければならなかった、パディングのボーダーは青い線を取り戻していました)。 。 –

1

this answer to a similar questionを見てください。基本的に、選択したアイテムの背景色が異なるようにリストボックスを再作成する場合は、選択したアイテムが目的の色を使用するように単に「HighlightBrush」カラーを変更する方が簡単です。

関連する問題