如何在UWP中将前景动态设置为主题资源?

时间:2018-08-17 02:12:57

标签: uwp

我有一个TextBlock,我想根据条件动态设置前景。但我希望前景可以成为主题资源。

<TextBlock Text="{Binding Contact.DisplayName}" Foreground="{Binding Converter={StaticResource isNewConverter}}" ></TextBlock>

public class IsNewConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {            
        IMessage message = value as IMessage;

        if (message == null || message.IsNew == false) return null;

        return SystemAccentColor; // where and how do I get the current SystemAccentColor brush?

    }

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

1 个答案:

答案 0 :(得分:2)

尝试一下:

return new SolidColorBrush( (Color)Application.Current.Resources["SystemAccentColor"]);

您可能还可以在资源中创建某种“ SystemAccentBrush”并将其颜色设置为SystemAccentColor,这样就不必在每次调用转换器时都创建新的画笔对象。

您可能还需要像SystemControlForegroundAccentBrush这样的几种系统画笔?因此,您可以这样做:

return (Brush)Application.Current .Resources["SystemControlForegroundAccentBrush"];

相关问题