Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have a method that it can verify high contrast white and high contrast black. It work well in my project. I hope it can help you.</p> <p>First, need new a DependencyProperty to judge if it is white or black.</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; namespace Views.Styles { public class HighConstrastWhite : DependencyObject { #region Singleton pattern private HighConstrastWhite() { SystemParameters.StaticPropertyChanged += SystemParameters_StaticPropertyChanged; } private static HighConstrastWhite _instance; public static HighConstrastWhite Instance { get { if (_instance == null) _instance = new HighConstrastWhite(); return _instance; } } #endregion public void ApplyCurrentTheme() { if(SystemParameters.HighContrast) { SolidColorBrush windowbrush = SystemColors.WindowBrush; if (windowbrush.Color.R == 255 &amp;&amp; windowbrush.Color.G == 255 &amp;&amp; windowbrush.Color.B == 255) HighConstrastWhite.Instance.IsHighContrastWhite = true; else HighConstrastWhite.Instance.IsHighContrastWhite = false; } } void SystemParameters_StaticPropertyChanged(object sender, PropertyChangedEventArgs e) { //Console.WriteLine(e.PropertyName); if (e.PropertyName == "HighContrast") { ApplyCurrentTheme(); } } #region DP IsHighContrast public static readonly DependencyProperty IsHighContrastWhiteProperty = DependencyProperty.Register( "IsHighContrastWhite", typeof(bool), typeof(HighConstrastWhite), new PropertyMetadata( false )); public bool IsHighContrastWhite { get { return (bool)GetValue(IsHighContrastWhiteProperty); } private set { SetValue(IsHighContrastWhiteProperty, value); } } #endregion } } </code></pre> <p>Second, you can use it in trigger. But you do best to use it with SystemParameters.HighContrast. For example:</p> <pre><code> ... xmlns:style="clr-namespace:Views.Styles" ... &lt;Style x:Key="FindTextImageButtonStyle" TargetType="controls:ImageButton" BasedOn="{StaticResource FunctionImageButtonStyle}"&gt; &lt;Setter Property="BorderThickness" Value="0,0,1,0"/&gt; &lt;Setter Property="NormalImage" Value="pack://application:,,,/App;component/Assets/Images/Find.png"/&gt; &lt;Setter Property="OverImage" Value="pack://application:,,,/App;component/Assets/Images/Find.png"/&gt; &lt;Setter Property="PressedImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Pressed.png"/&gt; &lt;Setter Property="DisableImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Disable.png"/&gt; &lt;Setter Property="Tag" Value="{DynamicResource {x:Static SystemParameters.HighContrastKey}}"/&gt; &lt;Style.Triggers&gt; &lt;MultiDataTrigger&gt; &lt;MultiDataTrigger.Conditions&gt; &lt;Condition Binding="{Binding Path=IsHighContrastWhite, Source={x:Static style:HighConstrastWhite.Instance}}" Value="True" /&gt; &lt;Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Value="true"/&gt; &lt;/MultiDataTrigger.Conditions&gt; &lt;Setter Property="NormalImage" Value="pack://application:,,,/App;component/Assets/Images/Find.scale-100_contrast-white.png"/&gt; &lt;Setter Property="OverImage" Value="pack://application:,,,/App;component/Assets/Images/Find.scale-100_contrast-white.png"/&gt; &lt;Setter Property="PressedImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Pressed.scale-100_contrast-white.png"/&gt; &lt;Setter Property="DisableImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Disable.png"/&gt; &lt;/MultiDataTrigger&gt; &lt;MultiDataTrigger&gt; &lt;MultiDataTrigger.Conditions&gt; &lt;Condition Binding="{Binding Path=IsHighContrastWhite, Source={x:Static style:HighConstrastWhite.Instance}}" Value="False" /&gt; &lt;Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Value="true"/&gt; &lt;/MultiDataTrigger.Conditions&gt; &lt;Setter Property="NormalImage" Value="pack://application:,,,/App;component/Assets/Images/Find.scale-100_contrast-black.png"/&gt; &lt;Setter Property="OverImage" Value="pack://application:,,,/App;component/Assets/Images/Find.scale-100_contrast-black.png"/&gt; &lt;Setter Property="PressedImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Pressed.scale-100_contrast-black.png"/&gt; &lt;Setter Property="DisableImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Disable.png"/&gt; &lt;/MultiDataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>By the way, For startup your app correctly in the high contrast theme, you need add code in your MainWindow.xaml.cs to trigger manually.</p> <pre><code>... public MainWindow() { HighConstrastWhite.Instance.ApplyCurrentTheme(); ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload