WPF组合框弹出模板我这里有我的个性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>
,但它不是由钩住DropDownOpened
或DropDownClosed
事件和刚刚从那里改变背景颜色工作
我其实有一个非常简单的跛脚的解决方案。该解决方案实际上工作得很好,但可能通过触发器或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 ...好..
你为什么不把SCB设置为像'scb = new SolidColorBrush(color);'这样的转换器?你的转换器是否进入过? –
是弗洛里安,完美执行。 –
那个触发器只是一个测试。这可以省略。对困惑感到抱歉。让我在触发器上删除它。 –