Skip to content

Resolve resources in binding conversion #2447

Discussion options

You must be logged in to vote

Hey @ProphetLamb,

you should be able to use TryGetValue and apply the value to the Foreground directly. I tested this with a little bit different sample, so you will need to adjust it for your case. But for the general idea, you can do something like this:

.Background(x => x.Binding(() => vm.IsToggleOn).Convert(ConvertIsOnToBrush))
    private Brush ConvertIsOnToBrush(bool isOn) 
    {
        return isOn ? GetBrush("PrimaryBrush") : GetBrush("TertiaryBrush");
    }

    private Brush GetBrush(string key)
    {
        if (Application.Current.Resources.TryGetValue(key, out var brush))
        {
            return brush as SolidColorBrush;
        }

        throw new KeyNotFoundException($"…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ProphetLamb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants