2013-12-12 2 views

답변

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(); 
...