Dot-Net

.net 4.5 下的列表/組合框背景和選定顏色

  • January 30, 2015

我有一個應用程序在 Windows 7 及更低版本上執行良好,目標是 .net 4 框架。

如果該應用程序現在安裝在 Windows 8 中(執行 .net 4.5 但仍以 .net 4 為目標),它會為列錶框或組合框中的選定項目顯示藍色背景,並為焦點項目顯示白色背景。有沒有辦法刪除這個?

我在我的 XAML 中使用以下內容來設置有問題的樣式,這似乎解決了 Windows 8 之前的問題。

<ListBox.ItemContainerStyle>
               <Style TargetType="{x:Type ListBoxItem}">
                   <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                   <Style.Resources>
                       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                       <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
                   </Style.Resources>
               </Style>
           </ListBox.ItemContainerStyle>

我忘了回來談談我是如何解決這個問題的……結果你需要做的就是創建一個空白項目容器樣式並將其分配給你的列錶框/組合框等……你可以使用它以及保持您可能正在使用的目前樣式,例如 ListBox Style="{StaticResource CurrentStyle}" ItemContainerStyle="{StaticResource BlankListBoxContainerStyle}" /> 其中 BlankListBoxContainerStyle 類似於…..

<Style x:Key="BlankListBoxContainerStyle" TargetType="{x:Type ListBoxItem}">
   <Setter Property="Template">
       <Setter.Value>
           <ControlTemplate TargetType="{x:Type ListBoxItem}">
               <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
           </ControlTemplate>
       </Setter.Value>
   </Setter>
   <Setter Property="FocusVisualStyle"
           Value="{x:Null}"/>
</Style>

引用自:https://stackoverflow.com/questions/12007918