-1
  1. create a usercontrol in WPF project.

     <UserControl x:Class="Test1.PostOperations"
              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" 
              xmlns:local="clr-namespace:Test1"
              mc:Ignorable="d" 
              d:DesignHeight="50" d:DesignWidth="500">
     <Grid>
         <StackPanel Orientation="Horizontal">
             <Image Name="Heart1"
                    Margin="8"
                    Source="pack://siteoforigin:,,, /Icons/nolike.png"
                    Stretch="Uniform"
                    MouseDown="Heart_MouseDown"
                    Width="40"
                    Height="40"
                    />
             <Image Name="Comment"
                    Margin="8"
                    Source="pack://siteoforigin:,,,/Icons/comment.png"
                    Stretch="Uniform"
                    Width="40"
                    Height="35"
                    />
             <Image Name="Send"
                    Margin="8"
                    Source="pack://siteoforigin:,,,/Icons/send.png"
                    Stretch="Uniform"
                    Width="40"
                    Height="35"
                    />
         </StackPanel>
         <Image Name="bookmark"
                    HorizontalAlignment="Right"
                    Margin="8"
                    Source="pack://siteoforigin:,,,/Icons/bookmark.png"
                    Width="40"
                    Height="35"
                    />
     </Grid>
    
    1. Images do appear in usercontrol.

    2. include the usercontrol in the project mainwindow but the image disappeared.

      enter <local:PostOperations Height="60"> </local:PostOperations> here

how to show the image in the usercontrol to a windows?

UpLevel
  • 1
  • 1
  • See here: https://stackoverflow.com/a/1651397/1136211. Set the Build Action of the image files to Resource and load them by [Referenced Assembly Resource File Pack URIs](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/app-development/pack-uris-in-wpf?view=netframeworkdesktop-4.8#referenced-assembly-resource-file). – Clemens Mar 25 '22 at 05:53

1 Answers1

0
pack://siteoforigin:,,,/Icons/send.png

is relative to the location from which the application's executable assembly is launched.

if your UserControl and MainWindow are not in the same assembly, make sure they are in the same path

sync
  • 223
  • 1
  • 7