Wpf 在解决方案资源管理器中的图像上,其中一个选项是“包含在项目中”。选择此选项后,一切正常。不知道为什么这个项目还没有包括在内,但希望这能帮助其他人!我还回到了相对路径('Images/company_128.png'),它可以正常工作。 <ImageS

Wpf 在解决方案资源管理器中的图像上,其中一个选项是“包含在项目中”。选择此选项后,一切正常。不知道为什么这个项目还没有包括在内,但希望这能帮助其他人!我还回到了相对路径('Images/company_128.png'),它可以正常工作。 <ImageS,wpf,visual-studio-2012,runtime-error,imagesource,Wpf,Visual Studio 2012,Runtime Error,Imagesource,在解决方案资源管理器中的图像上,其中一个选项是“包含在项目中”。选择此选项后,一切正常。不知道为什么这个项目还没有包括在内,但希望这能帮助其他人!我还回到了相对路径('Images/company_128.png'),它可以正常工作。 <ImageSource x:Key="CompanyIcon">Images/company_128.png</ImageSource> <Style x:Key="QuickInfoIcon" TargetType="{x:Ty

在解决方案资源管理器中的图像上,其中一个选项是“包含在项目中”。选择此选项后,一切正常。不知道为什么这个项目还没有包括在内,但希望这能帮助其他人!我还回到了相对路径('Images/company_128.png'),它可以正常工作。
<ImageSource x:Key="CompanyIcon">Images/company_128.png</ImageSource>

<Style x:Key="QuickInfoIcon" TargetType="{x:Type Image}">
    <!-- Default Value -->
    <Setter Property="Source" Value="{StaticResource CompanyIcon}" />
</Style>
<UserControl x:Class="SidekickAdmin.Views.QuickInfoView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignWidth="500" Height="100"
             Background="BlanchedAlmond">

    <!-- Setup a grid to house the icon and the info -->
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" Name="InfoIcon">
            <Image Style="{StaticResource QuickInfoIcon}" Height="50" Width="50"/>
        </Grid>
    </Grid>


</UserControl>
<BitmapSource x:Key="RedoImage">Images/Redo.png</BitmapSource>
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/WPFResourceLibrary;component/Resources/images.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
<Image x:Class="WPFResourceLibrary.Controls.ImageFromResource"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            >

</Image>
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WPFResourceLibrary.Controls
{
    /// <summary>
    /// ImageFromResource - an extension of the standard Image control that properly handles binding Resource to an Image from a Resource
    /// </summary>
    public partial class ImageFromResource : Image
    {
        public ImageFromResource()
        {
            InitializeComponent();


            DependencyPropertyDescriptor imageDescriptor = DependencyPropertyDescriptor.FromProperty(ImageFromResource.ResourceNameProperty, typeof(ImageFromResource));
            if (imageDescriptor != null)
            {
                imageDescriptor.AddValueChanged(this, delegate { SetImage(); });
            }
        }

        public static DependencyProperty ResourceNameProperty = DependencyProperty.Register("ResourceName", typeof(ImageSource), typeof(ImageFromResource), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        [Description("Resource String of the Target Image."), Category("Appearance")]
        public ImageSource ResourceName
        {
            get { return (ImageSource)GetValue(ResourceNameProperty); }
            set
            {
                SetValue(ResourceNameProperty, value);
            }
        }

        private void SetImage()
        {
            if(ResourceName != null)
                this.Source = new BitmapImage(new Uri(ResourceName.ToString()));
        }

    }
}