2013-01-31 64 views
0

WPF組合框彈出模板我這裏有我的個性ComboBox邊境改變背景顏色的每個系統的顏色改變

enter image description here

它實際上是一個瀏覽器選擇。但現在你看到的是使用ComboBox的選擇器控制原型。

我遇到的問題是這樣的下拉菜單(Popup)控制的背景

enter image description here

背景顏色應動態,一旦用戶改變改變自己的主題顏色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事件和剛剛從那裏改變背景顏色工作

我其實有一個非常簡單的跛腳的解決方案。該解決方案實際上工作得很好,但可能通過觸發器或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(); 

LOL ...好..

+0

你爲什麼不把SCB設置爲像'scb = new SolidColorBrush(color);'這樣的轉換器?你的轉換器是否進入過? –

+0

是弗洛裏安,完美執行。 –

+0

那個觸發器只是一個測試。這可以省略。對困惑感到抱歉。讓我在觸發器上刪除它。 –

回答

0

在我看來,第一步爲了得到這個特定顏色變化的通知,你需要在MainWindow中掛鉤WindowProc(或者一個隱藏的顏色變化檢測窗口?)並且觀察DWM中的WM_DWMCOLORIZATIONCOLORCHANGED消息。