2010-10-25 19 views
4

我有以下窗口,其中顯示了一個列表視圖。我定義了一個樣式爲一個ListViewItem:ResourceDictionary中的ListViewItem風格

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary> 
        <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> 
         <Style.Triggers> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="IsMouseOver" Value="True" /> 
            <Condition Property="IsSelected" Value="False"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" Value="Red" /> 
          </MultiTrigger> 
          <Trigger Property="IsSelected" Value="True"> 
           <Setter Property="Background" Value="Blue" /> 
          </Trigger> 
         </Style.Triggers> 
        </Style> 
       </ResourceDictionary> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Window.Resources> 
    <DockPanel> 
     <ListView x:Name="listView"/> 
    </DockPanel> 
</Window> 

後面的代碼被簡單地定義爲:

 public MainWindow() 
     { 
      InitializeComponent(); 
      for (int i = 1; i <= 100; i++) 
      { 
       listView.Items.Add(i); 
      } 
     } 

現在,當我運行應用程序,一切看起來除了在列表視圖中的第一項好。對於第一個項目,不應用任何樣式。如果我改變我的XAML的下方,消除資源字典相關的線,這一切工作正常:

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> 
      <Style.Triggers> 
       <MultiTrigger> 
        <MultiTrigger.Conditions> 
         <Condition Property="IsMouseOver" Value="True" /> 
         <Condition Property="IsSelected" Value="False"/> 
        </MultiTrigger.Conditions> 
        <Setter Property="Background" Value="Red" /> 
       </MultiTrigger> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="Background" Value="Blue" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <DockPanel> 
     <ListView x:Name="listView"/> 
    </DockPanel> 
</Window> 

我發現一些示例代碼在網上和這些資源字典行一對夫婦的編輯之後遺留下來的,但我不明白爲什麼如果他們只出現在視圖中的第一個項目不會選擇定義的樣式。

任何想法?

編輯:

我發現我的IsSelected背景顏色沒有被拾起。例如,如果我將其設置爲「綠色」,則所選項目仍將使用默認的Windows選定顏色。

回答

2

之前有這個問題,apperently的ListViewItem的選擇背景顏色必須

<Style.Resources> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black"/> 
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/> 
</Style.Resources> 

設置我從來沒有明白其中的原因。也許別人在這裏可以解釋。

對於第一部分,如果您將該樣式放在單獨的資源字典中,它將起作用,就像這樣。 (不知道爲什麼你得到你得到任何效果)

Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> 
     <Style.Resources> 
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/> 
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/> 
     </Style.Resources> 
     <Style.Triggers> 
      <MultiTrigger> 
       <MultiTrigger.Conditions> 
        <Condition Property="IsMouseOver" Value="True" /> 
        <Condition Property="IsSelected" Value="False"/> 
       </MultiTrigger.Conditions> 
       <Setter Property="Background" Value="Red" /> 
      </MultiTrigger> 
     </Style.Triggers> 
    </Style> 
</ResourceDictionary> 

主窗口

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Dictionary1.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 
<DockPanel Name="c_dockPanel"> 
    <ListView x:Name="listView"/> 
</DockPanel>