Using Resourse.resx in a WPF Application to Set Color

I am trying to create an application that should easily change a dll file that can change colors in the application. I am trying to use the resource manager for this, but I am having trouble adjusting the color values ​​so that the styles for the views can easily accept them. We know that (in this case) the background of the button accepts SolidColorBrush, and

Value="Black" works,
Value={x:Static res:AppResources.Btn_Background}

which gives the string Blackis not (current theory is that the transformers do the previous work, not the last). All this is done in wpf and mvvm. You guys know how to do this.

Hi

+3
source share
3 answers

You can use Binding:

Background="{Binding Source={x:Static res:AppResources.Btn_Background}}"

CoerceValue DependencyProperty, .

@Snowbear , Color, String, IValueConverter.

public class ColorConverter: IValueConverter
{
    #region IValueConverter Members

    private Dictionary<Color, Brush> brushes = new Dictionary<Color, Brush>();

    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        Brush brush;
        var color = (Color)value;
        if (!brushes.TryGetValue(color, out brush))
        {
            brushes[color] = brush = new SolidColorBrush(color);
            brush.Freeze();
        }

        return brush;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}
+1

, Brush .

, , , , , . , Mode = OneTime Binding.

MarkupExtension, .

, IValueConverter MarkupExtension, BrushConverter. , "" "# 000", , XAML, .

EDIT:

, StaticExtension, :

public class BrushStaticExtension : StaticExtension {

    private static BrushConverter converter = new BrushConverter();

    public BrushStaticExtension() { }
    public BrushStaticExtension(string member) : base (member) { }

    public override object ProvideValue(IServiceProvider serviceProvider) {
        return converter.ConvertFrom(base.ProvideValue(serviceProvider));
    }

}
+1

If you specify a string, the XAML parser uses a converter from the string that it automatically creates SolidColorBrush. As far as I understand, at the moment the resource Btn_Background Color, but it should be SolidColorBrush.

0
source

All Articles