2009-05-21 71 views
2

我玩弄WPF和我看到下面的文章: WPF ListView Inactive Selection Color列表視圖選擇顏色

我想要做類似的事情。我想在選中時在listviewitem周圍放置邊框,並且我不想更改背景顏色。我想要這個的原因是我想要一個顏色編碼的列表視圖,我仍然希望在選中時看到顏色,但是我想知道它是由它周圍的邊框選中的。

任何想法?

UPDATE:

我嘗試了下面的答案,它讓我半路上,它把周圍的邊框ListViewItem的,但它會覆蓋我的背景顏色。我不能得到正確的語法我嘗試(注意支持算法FMP):

<Style x:Key="SourceListView" TargetType="{x:Type ListViewItem}"> 
     <Setter Property="Background" Value="{Binding SourceType, Converter={StaticResource SourceGroupConverter}}"/> 
    </Style> 

    <Style x:Key="MyListViewItemStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource SourceListView}" > 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListViewItem}"> 
        <Border 
          x:Name="Border" 
          BorderBrush="Transparent" 
          BorderThickness="1"> 
         <GridViewRowPresenter Columns="{TemplateBinding GridView.ColumnCollection}" Content="{TemplateBinding Content}"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsSelected" Value="true"> 
          <Setter TargetName="Border" Property="BorderBrush" Value="Black"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

然後我嘗試這樣做:

<Style x:Key="MyListViewItemStyle" TargetType="{x:Type ListViewItem}"> 
     <Setter Property="Background" Value="{Binding SourceType, Converter={StaticResource SourceGroupConverter}}"/> 
     <Setter Property="Template"> 
      ...//Same as above 
     </Setter> 
    </Style> 

嘗試都只是將背景設置爲白色(或透明的我不知道)。我知道這只是語法,我會很欣賞在正確的方向另一個微調:)

回答

4

將ListView上的ItemContainerStyle更改爲不改變背景,當一個項目被選中,但改變顏色的風格邊境。下面是一個例子:

<Style x:Key="MyListViewItemStyle" TargetType="{x:Type ListViewItem}"> 
    <Setter Property="Background" Value="{Binding SourceType, Converter={StaticResource SourceGroupConverter}}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListViewItem}"> 
       <Border 
       x:Name="Border" 
       BorderBrush="Transparent" 
       BorderThickness="1" 
       Background="{TemplateBinding Background}"> 
       <GridViewRowPresenter Columns="{TemplateBinding GridView.ColumnCollection}" Content="{TemplateBinding Content}"/> 
       </Border> 
       <ControlTemplate.Triggers> 
       <Trigger Property="IsSelected" Value="true"> 
        <Setter TargetName="Border" Property="BorderBrush" Value="Black"/> 
       </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    </Style> 

然後用這樣的風格:

<ListView ItemContainerStyle="{StaticResource MyListViewItemStyle}"> 
    ... 
</ListView> 
+0

我嘗試這樣做,它沒有設定一個邊界,但似乎我的背景色語法被覆蓋。還有什麼幫助?另一件我注意到的事情是,如果你用上面的樣式點擊listviewitem的非空白區域(例如,單擊一列的內容),那麼listview纔會被選中。爲什麼是這樣? – Jose 2009-05-22 13:33:35