Code Red > .NET

Beginning to hate WPF Windows almost as much as WinForms!

(1/2) > >>

Jeff_M:
My transition to WPF has been a slow one, but I was starting to get in a groove and was actually enjoying it. Until I started spending way too much time trying to get my UserControls with controls with images to actually show those images in the windows that the controls are added to. Some windows are in a different project in the same solution, others are in the same project as the control. This is something that WinForms never had a problem with, yet in WPF it seems to be a very common problem (based on my Google searches). This is at run time as well as in the Designer.

I've tried every 'solution' I could find, all with the same end result of the image not displaying.  The Build Action for the images starts out as Embedded Resource, as that is what has been working for years for the WinForms\Controls. I've changed this, based on those 'solutions', to just Embedded, Content, and None, and Copy & Don't Copy. I've changed the Source= from being just a direct path to using the pack:// option. Both show the image in the UserControl but it disappears in a consuming Window. The images are all in the Project\Resources folder and I've even added them to the project Resources.resx

Is there a WPF deity I have to worship to get these to show up correctly?  :uglystupid2: :idiot2: :crazy2:

huiz:
I set the build action to Resource for images. But there might be other issues related to WPF. Can you share some XAML?

Jeff_M:
Oops, where I typed Embedded, Control, should have been Resource, Control.  Here is the XAML for a simple UserControl. The image is in the same project.

--- Code - XML: ---<UserControl x:Class="Quux.WPFControls.UserControls.SelectSurfaceControl"             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:Quux.WPFControls.UserControls"             mc:Ignorable="d"             d:DesignHeight="90" d:DesignWidth="300" d:Background="White">    <UserControl.Resources>        <Image x:Key="PickImage" Source="pack://application:,,,/Resources/PickInDrawing.png"/>    </UserControl.Resources>    <StackPanel Margin="5" HorizontalAlignment="Stretch">        <Grid HorizontalAlignment="Stretch">            <Grid.ColumnDefinitions>                <ColumnDefinition Width="*"/>                <ColumnDefinition Width="25"/>            </Grid.ColumnDefinitions>            <TextBlock x:Name="label"  Text="{Binding PromptString, FallbackValue='Surface:'}" Grid.Column="0" VerticalAlignment="Bottom" Margin="2"/>            <Button x:Name="b_SelectSurface" Grid.Column="1" Margin="2"  HorizontalAlignment="Right" Content="{StaticResource PickImage}" ToolTip="Pick Object on desired Layer"                    Command="{Binding PickSurfaceInDrawing}" IsEnabled="{Binding ContainsSurfaces}" MinWidth="16"/>        </Grid>        <ComboBox x:Name="cb_Surface" Grid.Row="1" Margin="2,5" ItemsSource="{Binding Surfaces, UpdateSourceTrigger=PropertyChanged}"                  SelectedValue="{Binding SelectedSurfaceName, UpdateSourceTrigger=PropertyChanged}"  IsEnabled="{Binding ContainsSurfaces}"/>        <TextBlock x:Name="label_ElevationRange" Text="{Binding SurfaceElevationRange, UpdateSourceTrigger=PropertyChanged, FallbackValue='Elevation range: 0.00-100.00'}" Margin="10,2" FontStyle="Italic"/>    </StackPanel></UserControl> 
Then, in the Window where the image does not display in the designer nor at runtime, still in the same project:

--- Code - XML: ---        <civilUC:SelectSurfaceControl /> 
In my main project I have a window to show information about the project. In that is an image which is in the same WPF project as above. This image shows in the designer but not at runtime.

--- Code - XML: ---            <Image Grid.Column="0" Height="Auto" Width="Auto" Stretch="Fill" Source="pack://application:,,,/Quux.WPFControls;component/Resources/QuuxAboutBoxImage.png"/> 

huiz:
I can have a deeper look later this weekend.
At first glance you miss the /component/ part in line 10. Also you reference the button content to an Image object. You probably add something like x:Share=false (am not sure about the right spelling) to the Image object, else it will only show once. Things like that makes me hating wpf from the bottom of my toes.

Jeff_M:
The /component/ isn't supposed to be needed when using the /Resources in the same project. However, adding the project's assembly and /component/ actually did work for this one control/window. It also helped with some other controls. But since I was already using the assembly in the Source for the Window in a different project, it is still not finding the image at run time.

I will keep plugging away at this. Thanks for your input!

Navigation

[0] Message Index

[#] Next page

Go to full version