2010-04-05 49 views
0

我有它<ListBox>定製<ListBox.ItemTemplate><DataTemplate>的DataTemplate,風格,觸發器

<ListBox> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Border BorderBrush="Black" BorderThickness="2" CornerRadius="5"> 
        <Image Source="{Binding Picture}" /> 
       </Border> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
</ListBox> 

現在,當我選擇了ListBoxItem它變得醜陋與藍色行選擇。我想改變它。我只想爲邊框的背景着色,沒有別的。我也想改變MouseOver的行爲。我試過槽觸發器,但ContentPresenter沒有Background屬性。

UPD:

嗯,我已經成功地改變MouseEnterMouseLeave背景:

<EventTrigger RoutedEvent="Border.MouseEnter"> 
     <BeginStoryboard> 
       <Storyboard > 
       <ColorAnimation Storyboard.TargetProperty="Background.Color" 
        To="LightBlue" Duration="0:0:0.03"/> 
       </Storyboard> 
     </BeginStoryboard> 
    </EventTrigger> 

不過還是當項目的選擇不能改變Background。我試圖通過:

<Trigger Property="ListBoxItem.IsSelected" Value="True"> 
     <Setter Property="Background" Value="Red" /> 
    </Trigger> 

不工作

回答

2

着色你要找的是兩個觸發模板ListBoxItem的,而不是裏面的ItemTemplate。要改變這個,你需要編輯ListBox的ItemContainerStyle。這是可以作爲一個起點,默認:

<ListBox> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="Background" Value="Transparent"/> 
       <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
       <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
       <Setter Property="Padding" Value="2,0,0,0"/> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
          <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> 
           <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
          </Border> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsSelected" Value="true"> 
            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
           </Trigger> 
           <MultiTrigger> 
            <MultiTrigger.Conditions> 
             <Condition Property="IsSelected" Value="true"/> 
             <Condition Property="Selector.IsSelectionActive" Value="false"/> 
            </MultiTrigger.Conditions> 
            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
           </MultiTrigger> 
           <Trigger Property="IsEnabled" Value="false"> 
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 
0

您可以通過使用觸發器做到這一點。我在我的項目之一是這樣的:

<Trigger Property="IsSelected" Value="true"> 
    <Setter Property="Panel.ZIndex" Value="1"/> 
    <Setter Property="Background" TargetName="Bd" Value="{StaticResource TabItemSelectedBackground}"/> 
</Trigger> 

雖然它不改變邊框顏色,它顯示如何更改背景。所以,也許嘗試將其設置爲null。此觸發器是使用IsMouseOver屬性實現跟蹤的自定義樣式的一部分。

HTH

+0

呀很好,它說: 找不到模板屬性「IsSelected」的類型「System.Windows.Controls.ContentPresenter」 – Agzam 2010-04-05 19:11:12

相關問題