IValueConverter轉換StaticResource引用的資源

[英]IValueConverter to convert a resource referenced by StaticResource


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?.

我希望使用IValueConverter與我從應用程序的資源獲取的值。我注意到幾年前在這里問了一個類似的問題:如何使用Converter綁定到StaticResource?

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

但是,將Source屬性更新為資源中的對象對我來說不起作用。我的具體案例:

<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.

在應用程序啟動的早期階段,AppThemeColor在后面的代碼中動態定義和設置。轉換器中的邏輯只是說使用提供的顏色,除非應用程序處於高對比度模式,在這種情況下它使用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:

編輯:有些人問我是如何動態設置AppThemeColor的。我只是在App.xaml.cs的OnActivatedAsync中添加了以下單行:

Application.Current.Resources[AppThemeColorResourceKey] = (themeExists) ? branding.ThemeColor : blueThemeColor;

1 个解决方案

#1


0  

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

您可以將這些StaticResources轉換為應用程序的“Style”類,並將其分配給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.

如果您的項目使用MVVM模式,請使用單例模式創建該類,並將其用作需要該樣式的ViewModel的屬性。

STYLE CLASS:

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

現在您需要將其分配給Window的DataContext

CODE:

碼:

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

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


ON XAML:

在XAML上:

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/06/01/725988814c2715e642465d2eafafacdb.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com