我做到了!
我的目標是更新我TabItems
基於當前窗口的標題/鉻色風格,不過由於Static
屬性未觸發我Triggers
,我不得不通過代碼來做到這一點。
由於我的應用可以將鍍鉻擴展到客戶區,因此在窗口顏色較暗時,某些Labels
可能難以閱讀。
這是我的工作的結果是:
光: ![Light Theme](https://i.stack.imgur.com/msH8z.png)
黑暗: ![Dark Theme](https://i.stack.imgur.com/5A9ZJ.png)
方法如下:我
使用StaticPropertyChanged
檢測窗口顏色變化:
private void SystemParameters_StaticPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "WindowGlassColor")
{
RibbonTabControl.UpdateVisual();
}
}
我必須創建一個定製TabItem
控件,其中包含一個名爲IsDark
的布爾屬性。
我TabControl
有一個公共的方法來更新IsDark
值:
public static bool UsesColor
{
get
{
try
{
//Start menu:
//HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\
//CurrentVersion\Themes\Personalize
var autoColorization =
Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\DWM",
"ColorPrevalence", "0").ToString();
return autoColorization.Equals("1");
}
catch (Exception)
{
return false;
}
}
}
獲取的給Color
亮度值:
public static int GetBrightness(this Color c)
{
//I discovered that 137 is the threshold, if more than that,
//the window title is white. Less than that, is black.
return (int)Math.Sqrt(
c.R * c.R * .241 +
c.G * c.G * .691 +
c.B * c.B * .068);
}
public void UpdateVisual()
{
//If glass isn't enabled, ignore.
var isDark = !SystemParameters.IsGlassEnabled
//Gets a registry value. See below.
|| !Glass.UsesColor
//Color threshold. See below.
|| SystemParameters.WindowGlassColor.GetBrightness() < 137;
//Manually update the IsDark property.
foreach (var tab in _tabPanel.Children.OfType<AwareTabItem>())
{
tab.IsDark = isDark;
}
}
如果Show Color on Start, Taskbar, Action Center
檢查獲取
最後,h ERE的我AwareTabItem
Style
:
<Style TargetType="{x:Type local:AwareTabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:AwareTabItem}">
<Grid Name="Panel" Background="Transparent">
<Border Name="ContentBorder" BorderBrush="#FFD4D4D4" BorderThickness="0">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center" Effect="{x:Null}"
HorizontalAlignment="Center"
ContentSource="Header" Margin="10,2"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True" SourceName="Panel">
<Setter Property="Foreground" Value="#FF2B579A" />
<Setter Property="Background" Value="#FFFAFAFA" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Panel" Property="Background" Value="#FFFAFAFA" />
<Setter Property="Foreground" Value="#FF2B579A" />
<Setter TargetName="ContentBorder" Property="BorderThickness" Value="1,1,1,0" />
</Trigger>
<!--When ExtendChrome, !IsDark, !IsSelected-->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Source={x:Static prop:Settings.Default}, Path=EditorExtendChrome, FallbackValue=False}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsDark}" Value="False"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelect}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="#FF000000"/>
<Setter TargetName="ContentBorder" Property="Background">
<Setter.Value>
<RadialGradientBrush>
<GradientStop Color="#9AFFFFFF" Offset="0"/>
<GradientStop Color="#90FFFFFF" Offset="0.4"/>
<GradientStop Offset="1"/>
</RadialGradientBrush>
</Setter.Value>
</Setter>
</MultiDataTrigger>
<!--When ExtendChrome, !IsDark, IsMouseOver-->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Source={x:Static prop:Settings.Default}, Path=EditorExtendChrome, FallbackValue=False}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsDark}" Value="False"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="#FF2B579A"/>
</MultiDataTrigger>
<!--When ExtendChrome, !IsDark, IsSelected-->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Source={x:Static prop:Settings.Default}, Path=EditorExtendChrome, FallbackValue=False}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsDark}" Value="False"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="Panel" Property="Background" Value="#FFFAFAFA" />
<Setter Property="Foreground" Value="#FF2B579A" />
<Setter TargetName="ContentBorder" Property="BorderThickness" Value="1,1,1,0" />
</MultiDataTrigger>
<!--When ExtendChrome, IsDark, !IsSelected-->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Source={x:Static prop:Settings.Default}, Path=EditorExtendChrome, FallbackValue=False}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsDark}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="#FFF8F8FF"/>
<Setter TargetName="ContentBorder" Property="Background">
<Setter.Value>
<RadialGradientBrush>
<GradientStop Color="{Binding Source={x:Static SystemParameters.WindowGlassColor},
Converter={StaticResource ColorToAlphaConverter}, ConverterParameter=6E}" Offset="0"/>
<GradientStop Color="{Binding Source={x:Static SystemParameters.WindowGlassColor},
Converter={StaticResource ColorToAlphaConverter}, ConverterParameter=50}" Offset="0.4"/>
<GradientStop Offset="1"/>
</RadialGradientBrush>
</Setter.Value>
</Setter>
</MultiDataTrigger>
<!--When ExtendChrome, IsDark, IsMouseOver-->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Source={x:Static prop:Settings.Default}, Path=EditorExtendChrome, FallbackValue=False}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsDark}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="#FFBFEFFF"/>
</MultiDataTrigger>
<!--When ExtendChrome, IsDark, IsSelected-->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Source={x:Static prop:Settings.Default}, Path=EditorExtendChrome, FallbackValue=False}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsDark}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="Panel" Property="Background" Value="#FFFAFAFA" />
<Setter Property="Foreground" Value="#FF2B579A" />
<Setter TargetName="ContentBorder" Property="BorderThickness" Value="1,1,1,0" />
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<!--Default Values-->
<Setter Property="FontFamily" Value="Segoe UI Semilight"/>
</Style>
我注意到,使用深色窗口顏色時,黑色RadialGradientBrush
有一個奇怪的效果,所以我使用的實際窗口的顏色,使柔和的背景(以提高可讀性當窗口太透明時)。要使用GradientStop
我必須創建一個Converter
,它取當前窗口的顏色並將給定的參數作爲alpha值。
ColorToAlpha轉換器:
您可以結合的目的,你明確你的'StaticPropertyChanged'事件處理程序設置創建一個新的屬性,或者你可以使用['BindingExpression.UpdateTarget()' ](https://msdn.microsoft.com/en-us/library/system.windows.data.bindingexpression.updatetarget(v = vs.110).aspx)在事件處理程序中的方法。要麼應該工作正常。你有什麼?如果您仍然需要幫助,請提供一個很好的[mcve],顯示您嘗試過的內容,並解釋您需要幫助的具體內容。 –
這篇文章包含了迄今爲止我嘗試過的所有內容。我不知道如何繼續。我不知道如何根據不會自行更新的'Static'值來更新Foreground。 –