2014-01-27 40 views
1

我正在更改我的ListView中的SystemColors.HighlightBrushKeySystemColors.ControlBrushKey,它工作正常;但是這個列表視圖在其每個ListViewItem(例如其他ListView,DataGrid)中都包含其他複雜控件,並且此新系統顏色將應用於所有子控件。重置覆蓋的SystemColors

<!-- This resource is added to remove the blue highlighting 
     in the selected ListView item. --> 
<SolidColorBrush 
     x:Key="{x:Static SystemColors.HighlightBrushKey}" 
     Color="Transparent" /> 

<!-- This resource is added to remove the background highlighting 
     of the inactive selected ListView item. --> 
<SolidColorBrush 
     x:Key="{x:Static SystemColors.ControlBrushKey}" 
     Color="Transparent" /> 

有沒有一種方法可以將此係統顏色重置爲原始的一個控件,該控件是此ListView的子項?

我面臨的實際問題是,這些重寫的系統顏色正在應用到子控件(ListView)的上下文菜單中;這工作正常,但使用Windows經典主題時,ContextMenus使用這些系統顏色,看起來很奇怪。所以我希望如果我可以將SystemColors重置爲原始的,那麼ContextMenu會正常工作。

有沒有其他方法可以解決這個問題?

+0

覆蓋ListViewItem的模板而不是重寫SystemColors。 –

+0

你想禁用MouseOver效果嗎? –

+0

感謝Rohit,這是可能的,但我有另一個ListView控件和DataGrid,它們也需要這種行爲;這是將這種行爲應用於所有人的最簡單方法,也是最容易維護而不會使其更復雜的方法。 – akjoshi

回答

1

您應該重寫ListViewItem的默認模板而不是重寫SystemColors。

覆蓋模板和刪除選擇,並從中鼠標懸停的觸發器這樣的:

<ListView> 
     <ListView.ItemContainerStyle> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="ListViewItem"> 
          <Border BorderThickness="{TemplateBinding Border.BorderThickness}" 
            Padding="{TemplateBinding Control.Padding}" 
            BorderBrush="{TemplateBinding Border.BorderBrush}" 
            Background="{TemplateBinding Panel.Background}" 
            Name="Bd" 
            SnapsToDevicePixels="True"> 
           <ContentPresenter Content="{TemplateBinding ContentControl.Content}" 
                ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
                ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" 
                HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
                VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
                SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 
          </Border> 
          <ControlTemplate.Triggers> 
           <Trigger Property="UIElement.IsEnabled" Value="False"> 
            <Setter Property="TextElement.Foreground" TargetName="Bd"> 
             <Setter.Value> 
              <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" /> 
             </Setter.Value> 
            </Setter> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </ListView.ItemContainerStyle> 
    </ListView> 

如果想申請多個ListView的,把這個資源App.Resources下,並使用需要的地方。

+0

謝謝,我已經成功嘗試了這裏提到的類似解決方案 - http://www.tewari.info/2011/04/29/remove-listboxitem-highlighting/;也尋找其他可用的選項。 – akjoshi

+0

好的。只有其他我能想到的方式是在子控制資源中重新覆蓋它們。 –