列表/组合框背景和选定的颜色.net 4.5

时间:2012-08-17 14:42:13

标签: .net wpf windows-8

我有一个应用程序,它在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>

6 个答案:

答案 0 :(得分:10)

我忘记回来了解我是如何解决这个问题的....原来你需要做的就是创建一个空白的Item Container Style并将它分配给你的listbox / combobox等....你可以用它作为以及保持您可能正在使用的当前样式,例如ListBox Style =“{StaticResource CurrentStyle}”ItemContainerStyle =“{StaticResource BlankListBoxContainerStyle}”/&gt; 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>

答案 1 :(得分:6)

您的应用程序会覆盖SystemColors.HighlightBrushKey(和其他系统密钥)的值。这适用于通过引用系统颜色来定义其前景/背景颜色的控件,就像在Win7默认主题(Aero)中一样。 但Win8默认主题(Aero2)以不同方式定义颜色。所以你的覆盖没有效果。

主题不需要使用系统颜色。他们碰巧在Win7 / Aero中这样做,但仅仅是因为系统颜色被认为是足够的。

希望这有帮助。

答案 2 :(得分:4)

重读框架兼容性文档我发现在编译.NET 4并在Windows 8上运行时实际上并不需要它。

http://msdn.microsoft.com/en-us/library/system.windows.frameworkcompatibilitypreferences%28v=vs.110%29.aspx

但是我仍然拥有Windows 7中没有的蓝色背景,我最终将问题跟踪到列表视图,该列表视图的样式与我的列表框不同。我发现我实际上并不需要为元素选择元素的能力,因此将列表视图更改为itemscontrol解决了我的问题。

另见:

http://connect.microsoft.com/VisualStudio/feedback/details/750655/selected-listboxitem-does-not-have-a-background-of-controlbrushkey-when-app-is-unfocused

答案 3 :(得分:0)

它被称为默认主题,它取决于系统,你真的不想搞砸它。您可以通过设置另一个Control.Template来覆盖它。

答案 4 :(得分:0)

<Style.Triggers>
                <Trigger Property="IsSelected" Value="true">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect Color="Red"
                              BlurRadius="0"
                              ShadowDepth="0" />
                        </Setter.Value>
                    </Setter>
                </Trigger>

答案 5 :(得分:0)

以下是我提出的不涉及更改系统颜色或控件模板的内容。只需将ListBox包装在一个新的UserControl中。

public partial class StyledListBox : UserControl
{
    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public object SelectedItem
    {
        get { return GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    public StyledListBox()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(StyledListBox), new FrameworkPropertyMetadata(null));
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(StyledListBox), new FrameworkPropertyMetadata(null));

    public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(StyledListBox), new FrameworkPropertyMetadata(null)
    {
        BindsTwoWayByDefault = true,
        DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
    });
}

XAML:

<UserControl x:Class="StyledListBox"

     <ListBox ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type common:StyledListBox}}}"
              SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type common:StyledListBox}}}">

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border>
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                                             Value="True">
                                    <Setter Property="Background" Value="Red" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>

                    <ContentPresenter ContentTemplate="{Binding ItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StyledListBox}}}" />
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</UserControl>

然后简单地使用包装器UserControl,就像它是ListBox一样。您想要控制的任何其他ListBox属性都可以像我的示例中的ItemsSourceSelectedItem一样简单地添加到包装器中。