【发布时间】:2014-07-01 12:16:40
【问题描述】:
我有一个 WPF 应用程序,我们需要根据配置文件设置重新设置该应用程序。 App.xaml 的简单版本是:
App.xaml
<Application>
...
<Application.Resources>
<ResourceDictionary x:Key="NormalMain">
<ImageBrush x:Key="BackgroundBrush" ImageSource="Images/welcome.png"/>
</ResourceDictionary>
<ResourceDictionary x:Key="AlternateMain">
<SolidColorBrush x:Key="BackgroundBrush" Color="LightGreen"/>
</ResourceDictionary>
</Application.Resources>
...
</Application>
我想根据配置文件设置在运行时加载资源字典。我已经绑定了绑定并且知道它可以工作,但是我无法确定合并这个特定字典的方法。
MainWindowView.xaml
<Window
(...)
d:DataContext="{d:DesignInstance designer:DesignMainWindowViewModel, IsDesignTimeCreatable=True}"
Background="{StaticResource BackgroundBrush}">
...
<Window.Resources>
//Load dictionary based on binding here?
</Window.Resources>
</Window>
MainWindowViewModel 很简单,例如:
MainWindowViewModel.cs
public class MainWindowViewModel {
public bool IsAlternate {get;set;}
}
有没有办法正确地做到这一点?
编辑:我不想在代码隐藏中进行加载,并且 PACK 语法在这种情况下似乎与引擎的构建方式不同。
编辑 2:我在 App.xaml 中使用此代码有点接近:
<StaticResource ResourceKey="{Binding Path=IsAlternate, Converter={StaticResource BoolToResourceKeyId}}" />
但是当我看到它时,它会导致我认为出现一堆奇怪的错误。
【问题讨论】:
标签: c# wpf xaml mvvm resourcedictionary