I have a WPF user control library project. In this same project I have a resource file, and I include it in my user control resources like this:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Styles/Globals.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
How can I access this resource from code behind? When I try to access Application.Current.Resources it is empty.
Thanks
EDIT: I realized this is because the resource is member of the user control and not a member of the application. (In the user control library project I can't add resources in the application level).
But, because I am using MVVM I don't have acces from my viewmodel to the user control, so how can I reach the style in the user control's resource?
You can access the styles like
Style style = Application.Current.FindResource("ResourceName") as Style;
where 'ResourceName' is from 'Styles/Globals.xaml'
Something like this worked for me:
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyLibraryNamespace;component/rdImages.xaml" />
<ResourceDictionary Source="pack://application:,,,/MyLibraryNamespace;component/rdColorImages.xaml" />
<ResourceDictionary Source="/rdStyles.xaml" />
<ResourceDictionary Source="/rdStringResourceEN.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
First two resource dictionaries are from external library (Styles, Drawing Brushes), last two are local (included in my project - Styles, Strings).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.