0

I am looking to use an IValueConverter with a value that I am getting from the resources of an application. I noticed that a similar question was asked a few years ago here: How to bind to a StaticResource with a Converter?.

However, updating the Source attribute to an object in the resources didn't work for me. My particular case:

<TextBlock Text="SampleText" Foreground="{Binding Source={StaticResource AppThemeColor}, Converter={StaticResource ThemeColorToBrushConverter}, ConverterParameter={StaticResource ApplicationForegroundThemeBrush}, Mode=OneWay}" />

The AppThemeColor is defined and set dynamically in the code behind at an early stage of the app launch. The logic in the converter simply says to use the color provided unless the app is in highcontrast mode, in which case it uses the brush supplied in the ConverterParameter.

Does anyone know of any pitfalls I might be running into here? There are no compile or run time errors. The text just doesn't appear and the converter's convert doesn't seem to be getting called.

EDIT: Some were asking how I was setting the AppThemeColor dynamically. I simply added the following one-liner here in the App.xaml.cs's OnActivatedAsync:

Application.Current.Resources[AppThemeColorResourceKey] = (themeExists) ? branding.ThemeColor : blueThemeColor;
Community
  • 1
  • 1
evve
  • 2,806
  • 2
  • 14
  • 11
  • Show us where you set the `AppThemeColor` in code ? – Abin Jun 01 '16 at 20:58
  • If it is set dynamically, have you tried using DynamicResource instead of StaticResource? – 15ee8f99-57ff-4f92-890c-b56153 Jun 01 '16 at 21:17
  • DynamicResource isn't an option for me in UWP apps (maybe should have clarified that in the beginning) though that really shouldn't be the issue. I'll update the question I asked with the one-liner where I set the AppThemeColor. – evve Jun 02 '16 at 13:30

1 Answers1

0

You can transform these StaticResources into an "Style" class for your app and assign it to the Window.DataContext.

I think that would be the best approach to your case.

If your project uses MVVM pattern, create that class using singleton pattern and use it as a property for your ViewModel that need that style.

STYLE CLASS:

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Media;

public class DefaultStyleClass : INotifyPropertyChanged
{
    private Brush _appThemeColor;
    public Brush AppThemeColor
    {
        get { return _appThemeColor; }
        set
        {
            if(value != _appThemeColor)
            {
                _appThemeColor = value;
                NotifyPropertyChanged();
            }
        }
    }

    public DefaultStyleClass()
    {
        AppThemeColor = Brushes.Red;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var propertyChanged = PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}


Now you need to assign this to the DataContext of the Window

CODE:

public DefaultStyleClass StyleContext;
public MainWindow()
{
    InitializeComponent();

    StyleContext = new DefaultStyleClass();
    DataContext = StyleContext;
}


ON XAML:

<TextBlock Text="SampleText",
           Foreground="{Binding AppThemeColor},
           Converter={StaticResource ThemeColorToBrushConverter},
           ConverterParameter={StaticResource ApplicationForegroundThemeBrush},
           Mode=OneWay}" />
Henryk Budzinski
  • 1,161
  • 8
  • 20