2012-12-17 129 views
1

如何將IsSelected觸發器添加到以下ListBox,這將改變BackgroundBorder,稱爲PlaceHolder。我不能通過在IsMouseOver旁邊添加IsSelected觸發器來完成此操作。我不想選擇整個ListBoxItem,只是Border。感謝任何幫助!更改ListBoxItem的選擇顏色

<ListBox> 
    <ListBox.Template> 
     <ControlTemplate> 
      <ItemsPresenter /> 
     </ControlTemplate> 
    </ListBox.Template> 

    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Rows="2" Columns="3"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <ListBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="ContentTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
          <Border CornerRadius="5" x:Name="PlaceHolder" BorderBrush="PapayaWhip" BorderThickness="1"> 
           <StackPanel Orientation="Horizontal" Width="148" Height="60"> 
            <Image Source="{Binding Image}"></Image> 
            <Label VerticalAlignment="Center" x:Name="title" FontSize="12" FontWeight="SemiBold" Foreground="Gray" Content="{Binding Title}"></Label> 
           </StackPanel> 
          </Border> 
         <DataTemplate.Triggers> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter TargetName="border" Property="Background" Value="Bisque"></Setter> 
          </Trigger> 
         </DataTemplate.Triggers> 
        </DataTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

回答

2

您必須綁定到ListBoxItem.IsSelected屬性。

這應該工作

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Style.Resources> 
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
     </Style.Resources> 
     <Setter Property="ContentTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <Border CornerRadius="5" x:Name="PlaceHolder" BorderBrush="PapayaWhip" BorderThickness="1"> 
         <StackPanel Orientation="Horizontal" Width="148" Height="60"> 
          <Image Source="{Binding Image}"></Image> 
          <Label VerticalAlignment="Center" x:Name="title" FontSize="12" FontWeight="SemiBold" Foreground="Gray" Content="{Binding Title}"></Label> 
         </StackPanel> 
        </Border> 
        <DataTemplate.Triggers> 
         <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True"> 
          <Setter TargetName="PlaceHolder" Property="Border.Background" Value="Red"/> 
         </DataTrigger> 
        </DataTemplate.Triggers> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ListBox.ItemContainerStyle> 
+0

太好了!謝謝 – Vlad

+0

添加代碼刪除默認的藍色突出顯示 –

+0

好的,我已經添加了它之前 – Vlad