0

我在这里有我的定制ComboBox

在此处输入图像描述

它实际上是一个浏览器选择。但是现在您看到的是选择器控件的原型,使用ComboBox.

我遇到的问题是这个下拉(Popup)控制背景

在此处输入图像描述

Color and Apperance一旦用户在设置中更改其主题颜色,背景颜色应动态更改Personalization

这是我的模板背后的故事

我可以Background使用简单的方法更改颜色Converter

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    int argbColor = (int)Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM", "ColorizationColor", null);
    var color = System.Drawing.Color.FromArgb(argbColor);

    SolidColorBrush scb = new SolidColorBrush();

    if (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 1) // Windows 7
    {
        //scb = new SolidColorBrush(Color.FromArgb((byte)((int)color.A / 3), color.R, color.G, color.B));
        scb = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
    }
    else if (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 2) // Windows 8 
    {
        scb = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
    }

    return scb;
}

像这样设置它:

<me:BackgroundConverter x:Key="BgConverter" />

并像这样实现它:

<ControlTemplate TargetType="{x:Type ComboBox}">
    <Grid x:Name="MainGrid" SnapsToDevicePixels="true">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
        </Grid.ColumnDefinitions>
        <Popup x:Name="PART_Popup" Grid.Column="0" AllowsTransparency="true" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
            <Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent">
                <Border x:Name="DropDownBorder" 
                    BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" 
                    BorderThickness="{Binding Converter={StaticResource BorderConverter}}" 
                    Background="{Binding Converter={StaticResource BgConverter}}" 
                    CornerRadius="0,0,12,12">

                    <ItemsPresenter x:Name="bn" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="Center" />
                </Border>
            </Themes:SystemDropShadowChrome>
        </Popup>
        <ToggleButton Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxReadonlyToggleButton}"/>
        <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="HasItems" Value="false">
            <Setter Property="Height" TargetName="DropDownBorder" Value="95"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            <Setter Property="Background" Value="#FFF4F4F4"/>
        </Trigger>
        <Trigger Property="IsGrouping" Value="true">
            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

检查Border x:Name="DropDownBorder"线路。现在,当用户更改主题颜色时,如何使其动态更改?我通过触发器尝试过:

<Trigger Property="IsDropDownOpen" Value="true">
    <!-- not really working -->
    <Setter Property="Background" TargetName="DropDownBorder" Value="{Binding Converter={StaticResource BgConverter}}"/>
</Trigger>

但它不工作

我实际上有一个非常简单的蹩脚解决方案,方法是挂钩DropDownOpenedDropDownClosed事件,然后从那里更改背景颜色。该解决方案实际上运行良好,但可能有一种最简单的方法可以通过 Trigger 或 EventTrigger 来实现?

- 更新 -

public static class ComboBoxExtension
{
    public static void UpdatePopupBackground(this ComboBox cb)
    {
        Border b = (Border)cb.Template.FindName("DropDownBorder", cb);

        int argbColor = (int)Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM", "ColorizationColor", null);
        var color = System.Drawing.Color.FromArgb(argbColor);

        SolidColorBrush scb = new SolidColorBrush();

        if (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 1) // Windows 7
        {
            //scb = new SolidColorBrush(Color.FromArgb((byte)((int)color.A / 3), color.R, color.G, color.B));
            scb = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
        }
        else if (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 2) // Windows 8 
        {
            scb = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
        }

        b.Background = scb;
    }
}

并在 DropDownOpened 触发时调用它:

ComboMe.UpdatePopupBackground();

大声笑……嗯……

4

1 回答 1

0

在我看来,获得此特定颜色更改通知的第一步,您需要在 MainWindow 中挂钩 WindowProc(或隐藏的颜色更改检测窗口?)并注意来自 DWM 的 WM_DWMCOLORIZATIONCOLORCHANGED 消息。

于 2013-02-03T01:04:37.337 回答