Aliasing Resources (WPF)

后端 未结 2 975
旧时难觅i
旧时难觅i 2021-01-20 17:10

I am trying to alias a resource in XAML, as follows:



    

        
相关标签:
2条回答
  • 2021-01-20 17:50

    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.

    0 讨论(0)
  • 2021-01-20 17:58

    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.

    0 讨论(0)
提交回复
热议问题