WPF ComboBox弹出模板,边框改变背景颜色,每个系统颜色都有变化

时间:2013-01-31 09:55:53

标签: c# wpf combobox popup ivalueconverter

我在这里定制了ComboBox

enter image description here

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

我遇到的问题是此下拉列表(Popup)控制背景

enter image description here

用户在Color and Apperance的{​​{1}}设置中更改主题颜色后,背景颜色应动态更改。

这是我的模板背后的故事

我可以使用简单的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> DropDownOpened个事件,然后从那里更改背景颜色。该解决方案实际上工作得很好,但是通过Trigger或EventTrigger可能有一种最简单的方法吗?

- 更新 -

DropDownClosed

并在DropDownOpened触发时调用它:

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;
    }
}
大笑......好吧..

1 个答案:

答案 0 :(得分:0)

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

相关问题