I am trying to alias a resource in XAML, as follows:
<UserControl.Resources>
<StaticResourceExtension x:Key="newName" ResourceKey="oldName"/>
</UserControl.Resources>
oldName
simply refers to a resource of type Image
, defined in App.xaml
.
As far as I understand, this is the correct way to do this, and should work fine. However, the XAML code gives me the superbly unhelpful error:
"The application XAML file failed to load. Fix errors in the application XAML before opening other XAML files."
This appears when I hover over the StaticResourceExtension
line in the code (which has a squiggly underline). Several other errors are generated in the actual Error List, but seem to be fairly irrelevant and nonsenical (such messages as "The name 'InitializeComponent' does not exist in the current context"), as they all disappear when the line is removed.
I'm completely stumped here. Why is WPF complaining about this code? Any ideas as to a resolution please?
Note: I'm using WPF in .NET 3.5 SP1.
Update 1:
I should clairfy that I do receive compiler errors (the aforementioned messages in the Error List), so it's not just a designer problem.
Update 2:
Here's the relevant code in full...
In App.xaml (under Application.Resource
):
<Image x:Key="bulletArrowUp" Source="Images/Icons/bullet_arrow_up.png" Stretch="None"/>
<Image x:Key="bulletArrowDown" Source="Images/Icons/bullet_arrow_down.png" Stretch="None"/>
And in MyUserControl.xaml (under UserControl.Resources
):
<StaticResourceExtension x:Key="columnHeaderSortUpImage" ResourceKey="bulletArrowUp"/>
<StaticResourceExtension x:Key="columnHeaderSortDownImage" ResourceKey="bulletArrowDown"/>
These are the lines that generate the errors, of course.
Try as I might, I cannot reproduce your problem. I think there is more at play than the code you have posted. This works fine for me:
App.xaml:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<Image x:Key="bulletArrowUp" Source="Leaves.jpg" Stretch="None"/>
<Image x:Key="bulletArrowDown" Source="Leaves.jpg" Stretch="None"/>
</Application.Resources>
</Application>
Window1.xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<local:UserControl1/>
</Window>
UserControl1.xaml:
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<StaticResourceExtension x:Key="columnHeaderSortUpImage" ResourceKey="bulletArrowUp"/>
<StaticResourceExtension x:Key="columnHeaderSortDownImage" ResourceKey="bulletArrowDown"/>
</UserControl.Resources>
<ContentControl Content="{StaticResource columnHeaderSortUpImage}"/>
</UserControl>
I added the Leaves.jpg image to my project and it displayed just fine.
I tried using the same setup as Kent and received the following error:
Property 'Resources' does not support values of type 'System.Windows.Controls.Image'.
I also tried a few other types and got the same thing (only with a different full-qualified type name).
I was only able to re-key an Image in the <UserControl.Resources>
in the following way:
<UserControl.Resources>
<Image x:Key="myimg"
Source="{Binding Source={StaticResource appimg}, Path=Source}"
Stretch="{Binding Source={StaticResource appimg}, Path=Stretch}"/>
</UserControl.Resources>
I have to wonder: Why? Resources weren't designed to be re-keyed. I have a suspicion that what you are ultimately trying to accomplish could be done better and easier in some other way. Possibly with setters in a style.