我的 colorPalette.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>
答案 0 :(得分:1)
加载目录后,你可以循环它的项目并通过使用键更改它们的属性它应该是这样的:
foreach(object keyy in RescourcesDir.Keys)
{
//get the object and it's value
object val = RescourcesDir[keyy];
//change it's value ...
RescourcesDir[keyy] = somevalue;
}
try to take a look at this thread it may help you getting your rescourcesdirectory
答案 1 :(得分:0)
假设您有一个名为 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 any of these methods - 然后将其转换为画笔:
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();
}
}
}
注意:灰度颜色的反转不那么引人注目,所以对于这个例子我添加了:
<Color x:Key="Blue">#0000ff</Color>
<Color x:Key="Yellow">#ffff00</Color>
然后在xaml中添加你的引用:
xmlns:converters="clr-namespace:WPFTest.Converters"
声明你的钥匙:
<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}}"/>