【问题标题】:WPF ComboBox Popup template with border changing background color every system color changesWPF ComboBox Popup 模板与边框更改背景颜色每个系统颜色更改
【发布时间】:2013-01-31 09:55:52
【问题描述】:

我这里有我自定义的ComboBox


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

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

一旦用户在Color and Apperance 设置Personalization 更改他们的主题颜色,背景颜色应该会动态变化。

这是我的模板背后的故事

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

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();

哈哈……好吧……

【问题讨论】:

  • 你为什么不像scb = new SolidColorBrush(color);那样在转换器中设置你的SCB?你的转换器输入过吗?
  • 是的,弗洛里安,完美执行。
  • 触发器上的那个只是一个测试。那可以省略。对困惑感到抱歉。让我在触发器上删除它。
  • 如果您使用触发器并在转换器中设置随机颜色,颜色会改变吗?
  • 不,它没有。颜色仅从 Border 中的“Background="{Binding Converter={StaticResource BgConverter}}" 更改一次

标签: c# wpf combobox popup ivalueconverter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-09-17
    • 2012-04-11
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 2019-03-03
    • 2022-01-21
    • 2013-01-15
    • 2019-05-15
    相关资源
    最近更新 更多