2009-04-27 133 views
14

對不起,如果這之前已被問到,但我找不到解決方案,我在尋找在彈出的相關問題,或在谷歌。WPF列表框選擇顏色

在我的應用程序中,我試圖重新創建單詞新文檔對話框,項目左側的列表和右側的文本下方的圖標。在Word中,當鼠標懸停時,它具有橙色漸變,並且在選擇某個項目時具有較暗的漸變。我已經獲得了大部分重新創建的內容,除非您選擇一個項目後更改背景顏色。下面是我使用創建此代碼:

<ListView Margin="236,34,17,144" Name="listView1" HorizontalContentAlignment="Stretch"> 
     <ListView.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Columns="5" IsItemsHost="True" VerticalAlignment="Top" > 
       </UniformGrid> 
      </ItemsPanelTemplate> 
     </ListView.ItemsPanel> 
     <ListView.ItemTemplate> 
      <DataTemplate > 
       <StackPanel HorizontalAlignment="Center" Width="auto"> 
        <Image Source="images/document32.png" HorizontalAlignment="Center"/> 
        <TextBlock Text="{Binding}" HorizontalAlignment="Center" /> 
       </StackPanel> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
     <ListView.ItemContainerStyle> 
      <Style TargetType="{x:Type ListViewItem}" >     
       <Style.Triggers> 
        <Trigger Property="IsSelected" Value="true"> 
         <Setter Property="Foreground" Value="Yellow" /> 
         <Setter Property="Background" Value="Orange" /> 
        </Trigger> 
        <Trigger Property="IsMouseOver" Value="true"> 
         <Setter Property="Foreground" Value="Black" /> 
         <Setter Property="Background"> 
          <Setter.Value> 
           <LinearGradientBrush EndPoint="0.5,1" StartPoint="1,0"> 
            <GradientStop Color="#d3e7ff" Offset="0.986"/> 
            <GradientStop Color="#b0d2fc" Offset="0.5"/> 
            <GradientStop Color="#8ec1ff" Offset="0.51"/> 
           </LinearGradientBrush> 
          </Setter.Value> 
         </Setter> 
        </Trigger> 

       </Style.Triggers> 
      </Style> 
     </ListView.ItemContainerStyle> 
    </ListView> 

所以這會在看我要去了,在做鼠標,當我在列表視圖中選擇一個項目,將改變字體的文本黃色,但它拒絕將背景從默認藍色更改爲橙​​色,理想情況下它將是另一種漸變,而不是填充顏色。謝謝你的幫助。

回答

31

有一些黑客可以像覆蓋系統顏色鍵那樣做,但很可能您會希望提供一個新模板來實現此目的。這裏有一個相當漂亮的我放在一起:

<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="{StaticResource NormalItemBackground}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Grid> 
        <Border Background="{TemplateBinding Background}" /> 
        <Border Background="#BEFFFFFF" Margin="3,1"> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition /> 
           <RowDefinition /> 
          </Grid.RowDefinitions> 
          <Border Margin="2,1,2,0" Grid.Row="0" Background="#57FFFFFF" /> 
         </Grid> 
        </Border> 
        <ContentPresenter Margin="8,5" /> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsMouseOver" Value="True" /> 
          <Condition Property="IsSelected" Value="False"/> 
         </MultiTrigger.Conditions> 
         <Setter Property="Background" Value="{StaticResource HotItemBackground}" /> 
        </MultiTrigger> 
        <Trigger Property="IsSelected" Value="True"> 
         <Setter Property="Background" Value="{StaticResource SelectedItemBackground}" /> 
        </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>