2013-06-11 63 views
1

我正在對在相關的stackoverflow帖子中找到的代碼進行一些修改。我使用觸發器對IsMouseOver和IsSelected上的ListBoxItem的背景進行了輕微更改。在我的版本中,我想爲背景使用漸變:WPF在觸發器中設置ListBoxItem背景

<Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Margin" Value="1,2,1,1"/> 
     <Setter Property="HorizontalAlignment" Value="Stretch" /> 
     <Setter Property="Background" Value="White" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
        <Grid> 
         <Border Background="{TemplateBinding Background}" /> 
         <Border Background="LightGray" Margin="0,0"> 
          <Grid> 
           <Grid.RowDefinitions> 
            <RowDefinition /> 
            <RowDefinition /> 
           </Grid.RowDefinitions> 
           <!--<Border Margin="2,1,2,0" Grid.Row="0" Background="#57FFFFFF" />--> 
           <Border Margin="2,1,2,0" Grid.Row="0"> 
            <Border.Background > 
             <LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" > 
              <GradientStop Color="White" Offset="0" /> 
              <GradientStop Color="LightGray" Offset="1" /> 
             </LinearGradientBrush> 
            </Border.Background> 
           </Border> 
          </Grid> 
         </Border> 
         <ContentPresenter Margin="0,5" /> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="IsMouseOver" Value="True" /> 
           <Condition Property="IsSelected" Value="False"/> 
          </MultiTrigger.Conditions> 
          <!--<Setter Property="Background" Value="#CCCBAF00" /> 
          <Setter Property="Opacity" Value="0.8" />--> 
          <Setter Property="Background"> 
           <Setter.Value> 
            <LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" Opacity="0.8"> 
             <GradientStop Color="#CCC9BA5C" Offset="0" /> 
             <GradientStop Color="#CCCBAF00" Offset="1" /> 
            </LinearGradientBrush> 
           </Setter.Value> 
          </Setter> 
         </MultiTrigger> 
         <Trigger Property="IsSelected" Value="True"> 
          <!--<Setter Property="Background" Value="#CCCB6400" /> 
          <Setter Property="Opacity" Value="0.8" />--> 
          <Setter Property="Background"> 
           <Setter.Value> 
            <LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" Opacity="0.8"> 
             <GradientStop Color="#CCCD8B4C" Offset="0" /> 
             <GradientStop Color="#CCCB6400" Offset="1" /> 
            </LinearGradientBrush> 
           </Setter.Value> 
          </Setter> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}"> 
     <Setter Property="ItemContainerStyle" Value="{DynamicResource ListboxItemStyle}" /> 
     <Setter Property="Margin" Value="3,3,2,1" /> 
    </Style> 

但它不適用於此修改。謝謝!

回答

1

只需切換

<Border Background="LightGray" Margin="0,0"> 

喜歡的東西

<Border Background="LightGray" Margin="0,0" Opacity="0.5"> 

使^^ Border看穿

+0

謝謝!這是完美的。 – zzMzz

1

我可以看到你有一個邊框控件重疊另一個。第一個邊框(其背景與模板綁定)將永遠不可見。所以,當你改變ListBoxItem的背景時,你不會看到它,因爲它隱藏在另一個邊界之下。 您可以只有一個邊框控件,或者將第二個邊框控件的可見性設置爲隱藏在觸發器中。

相關問題