如何在wpf中为黑白高对比度模式设置不同的样式

时间:2013-12-12 07:30:11

标签: c# wpf xaml high-contrast

我使用此方法在xaml中设置高对比度样式:

 <DataTrigger Binding="{Binding Source={x:Static SystemParameters.HighContrast}}" Value="True">
   ...                    
 </DataTrigger>

但是有两个主要的高对比度模式,黑色和白色,如何分别设置这两种模式的风格?

1 个答案:

答案 0 :(得分:0)

我有一种方法可以验证高对比度白色和高对比度黑色。它在我的项目中运作良好。我希望它可以帮到你。

首先,需要新的DependencyProperty来判断它是白色还是黑色。

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 && windowbrush.Color.G == 255 && 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

}
}

其次,你可以在触发器中使用它。但您最好将它与SystemParameters.HighContrast一起使用。例如:

    ...
xmlns:style="clr-namespace:Views.Styles"
...


<Style x:Key="FindTextImageButtonStyle" TargetType="controls:ImageButton" BasedOn="{StaticResource FunctionImageButtonStyle}">
        <Setter Property="BorderThickness" Value="0,0,1,0"/>
        <Setter Property="NormalImage" Value="pack://application:,,,/App;component/Assets/Images/Find.png"/>
        <Setter Property="OverImage" Value="pack://application:,,,/App;component/Assets/Images/Find.png"/>
        <Setter Property="PressedImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Pressed.png"/>
        <Setter Property="DisableImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Disable.png"/>
        <Setter Property="Tag" Value="{DynamicResource {x:Static SystemParameters.HighContrastKey}}"/>
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Path=IsHighContrastWhite, Source={x:Static style:HighConstrastWhite.Instance}}" Value="True" />
                    <Condition  Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Value="true"/>
            </MultiDataTrigger.Conditions>
                <Setter Property="NormalImage" Value="pack://application:,,,/App;component/Assets/Images/Find.scale-100_contrast-white.png"/>
                <Setter Property="OverImage" Value="pack://application:,,,/App;component/Assets/Images/Find.scale-100_contrast-white.png"/>
                <Setter Property="PressedImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Pressed.scale-100_contrast-white.png"/>
                <Setter Property="DisableImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Disable.png"/>
            </MultiDataTrigger>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Path=IsHighContrastWhite, Source={x:Static style:HighConstrastWhite.Instance}}" Value="False" />
                    <Condition  Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Value="true"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="NormalImage" Value="pack://application:,,,/App;component/Assets/Images/Find.scale-100_contrast-black.png"/>
                <Setter Property="OverImage" Value="pack://application:,,,/App;component/Assets/Images/Find.scale-100_contrast-black.png"/>
                <Setter Property="PressedImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Pressed.scale-100_contrast-black.png"/>
                <Setter Property="DisableImage" Value="pack://application:,,,/App;component/Assets/Images/Find_Disable.png"/>
            </MultiDataTrigger>
        </Style.Triggers>


    </Style>

顺便说一句,要在高对比度主题中正确启动您的应用,您需要在MainWindow.xaml.cs中添加代码以手动触发。

...
public MainWindow()
        {
            HighConstrastWhite.Instance.ApplyCurrentTheme();
...