Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在wpf中为黑白高对比度模式设置不同的样式_C#_Wpf_Xaml_High Contrast - Fatal编程技术网

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

C# 如何在wpf中为黑白高对比度模式设置不同的样式,c#,wpf,xaml,high-contrast,C#,Wpf,Xaml,High Contrast,我使用此方法在xaml中设置高对比度样式: <DataTrigger Binding="{Binding Source={x:Static SystemParameters.HighContrast}}" Value="True"> ... </DataTrigger> ... 但是有两种主要的高对比度模式,黑色和白色,如何分别设置这两种模式的样式?我有一种方法可以验证高对比度

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

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

...                    

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

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

首先,需要一个新的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>

这是用于WinRT Metro应用程序还是仅用于WPF?
...
public MainWindow()
        {
            HighConstrastWhite.Instance.ApplyCurrentTheme();
...