2011-09-16 74 views
0

我想设计一些不错的列表框,但每次我点击列表框中的项目时 选择背景被破坏了一切... 我怎么能使它不相关?列表框设计问题

这是XAML:

<!-- Site List View --> 
<ListBox Grid.Row="1" Name="SiteListBox" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch"> 
    <ListBox.Background> 
     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
      <GradientStop Color="White" Offset="0" /> 
      <GradientStop Color="Gainsboro" Offset="0.805" /> 
     </LinearGradientBrush> 
    </ListBox.Background> 
    <ListBox.ItemContainerStyle> 
     <Style> 
      <Setter Property="Control.Padding" Value="0"></Setter> 
      <Style.Triggers> 
       <Trigger Property="ListBoxItem.IsSelected" Value="True"> 
        <Setter Property="ListBoxItem.Background" Value="DarkRed" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
       <Border Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="4" 
         Background="{Binding Path=Background, RelativeSource={ 
         RelativeSource 
         Mode=FindAncestor, 
         AncestorType={x:Type ListBoxItem} 
         }}"> 
        <TextBlock Margin="5" Text="{Binding Path=Name}" /> 
       </Border> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

一个也连接一个打印屏幕。 enter image description here

+2

您最好的选择是重新定义选定项目的颜色。看到这里:http://stackoverflow.com/questions/388232/wpf-listbox-image-selected-the-saga-continues/388313#388313 –

+0

@Matt汉密尔顿:刚才注意到你回答了同样的事情,但我在一条评论。你应该发布了一个答案,而不是意味着要窃取你的答案:) –

+0

@Meleak没想到这是值得复制的内容。 –

回答

0

您可以更改ListBoxItem容器IsSelected的默认颜色。将它们设置为Transparent即可获得您之后的效果

<ListBox ...> 
    <ListBox.Resources> 
     <!-- Brush for selected item that doesn't have focus --> 
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
     <!-- Brush for selected item that has focus --> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
    </ListBox.Resources> 
    <!-- ... --> 
</ListBox>