Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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 - Fatal编程技术网

C# 以编程方式反转静态资源字典颜色wpf

C# 以编程方式反转静态资源字典颜色wpf,c#,wpf,xaml,C#,Wpf,Xaml,我的调色板.xaml文件中有以下颜色,我很想知道 我是否可以通过编程在资源字典中循环颜色并反转颜色值 类似于在photoshop中拍摄图像并添加反转过滤器。我这样做是因为我不希望在手动反转颜色的情况下复制xaml文件。我宁愿它是一个更程序化的解决方案 <Color x:Key="Accent01">#1d7b87</Color> <Color x:Key="Accent02">#28aabc</Color> <Color x:Key="Co

我的调色板.xaml文件中有以下颜色,我很想知道

我是否可以通过编程在资源字典中循环颜色并反转颜色值

类似于在photoshop中拍摄图像并添加反转过滤器。我这样做是因为我不希望在手动反转颜色的情况下复制xaml文件。我宁愿它是一个更程序化的解决方案

<Color x:Key="Accent01">#1d7b87</Color>
<Color x:Key="Accent02">#28aabc</Color>

<Color x:Key="ColorWhite">White</Color>
<Color x:Key="ColorBlack">Black</Color>

<Color x:Key="Color01">#e0e0e0</Color>
<Color x:Key="Color02">#c3c5c7</Color>
<Color x:Key="Color03">#a6a9ad</Color> 
<Color x:Key="Color04">#8b8f94</Color>
<Color x:Key="Color05">#71757a</Color>
<Color x:Key="Color06">#585c61</Color>
<Color x:Key="Color07">#404347</Color>
<Color x:Key="Color08">#292b2e</Color>
<Color x:Key="Color09">#1e1f21</Color>
<Color x:Key="Color10">#121314</Color>
#1d7b87
#公元前28年
白色
黑色
#e0e0e0
#c3c5c7
#a6a9ad
#8b8f94
#71757a
#585c61
#404347
#292b2e
#1e1f21
#121314

加载目录后,您可以在其项上循环,并使用keys属性更改其值。它应该是这样的:

foreach(object keyy in RescourcesDir.Keys)
{
    //get the object and it's value
    object val = RescourcesDir[keyy];
    //change it's value ...
    RescourcesDir[keyy] = somevalue;
}

如果您不介意更改值,Ahmad的解决方案非常好。但是,如果您想保留原始值并使用反向版本,该怎么办

假设您有一个名为Converters的文件夹,并在其中创建以下两个
IValueConverter
类:

  • System.Windows.Media.Color
    转换为
    System.Windows.Media.SolidColorBrush
    的基类:

    using System;
    using System.Globalization;
    using System.Windows.Data;
    using System.Windows.Media;
    
    namespace WPFTest.Converters
    {
      public class ColorToBrush : IValueConverter
      {
        public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
          return value is Color ? new SolidColorBrush((Color)value) : Brushes.Black;
        }
    
        public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
          throw new NotImplementedException();
        }
      }
    }
    
  • 用于反转颜色的继承类--然后将其转换为画笔:

    using System;
    using System.Globalization;
    using System.Windows.Media;
    
    namespace WPFTest.Converters
    {
      public class InvertColorToBrush : ColorToBrush
      {
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
          if (value is Color)
          {
            Color color = (Color)value;
            int iCol = ((color.A << 24) | (color.R << 16) | (color.G << 8) | color.B) ^ 0xffffff;
            Color inverted = Color.FromArgb((byte)(iCol >> 24),
                                            (byte)(iCol >> 16),
                                            (byte)(iCol >> 8),
                                            (byte)(iCol));
    
            return base.Convert(inverted, targetType, parameter, culture);
          }
          else
          {
            return Brushes.Black;
          }
        }
    
        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
          throw new NotImplementedException();
        }
      }
    }
    
    申报您的钥匙:

    <converters:ColorToBrush x:Key="BrushColor" />
    <converters:InvertColorToBrush x:Key="BrushInvertColor" />
    
    
    
    使用方法:

    <Label
      Content="COLOR TEST"
      Background="{Binding Converter={StaticResource BrushColor}, Mode=OneWay, Source={StaticResource Blue}}"
      Foreground="{Binding Converter={StaticResource BrushColor}, Mode=OneWay, Source={StaticResource Yellow}}"/>
    
    <Label
      Content="COLOR INVERT TEST"
      Background="{Binding Converter={StaticResource BrushInvertColor}, Mode=OneWay, Source={StaticResource Blue}}"
      Foreground="{Binding Converter={StaticResource BrushInvertColor}, Mode=OneWay, Source={StaticResource Yellow}}"/>
    
    
    

    <converters:ColorToBrush x:Key="BrushColor" />
    <converters:InvertColorToBrush x:Key="BrushInvertColor" />
    
    <Label
      Content="COLOR TEST"
      Background="{Binding Converter={StaticResource BrushColor}, Mode=OneWay, Source={StaticResource Blue}}"
      Foreground="{Binding Converter={StaticResource BrushColor}, Mode=OneWay, Source={StaticResource Yellow}}"/>
    
    <Label
      Content="COLOR INVERT TEST"
      Background="{Binding Converter={StaticResource BrushInvertColor}, Mode=OneWay, Source={StaticResource Blue}}"
      Foreground="{Binding Converter={StaticResource BrushInvertColor}, Mode=OneWay, Source={StaticResource Yellow}}"/>