views:

113

answers:

2

I have a User Control that has one child - an Image. I'm trying to set a default image using code in the User Control's default constructor to display an image resource, but so far with no success in either Blend preview or when I actually use it in a running app. I do not get any errors either. Shouldn't this be possible?

Here is the XAML usage:

<MyNS:TestIcon
  x:Name="m_TestIcon"
/>

Here is the XAML for the User Control:

<UserControl
  x:Class="MyNS.TestIcon"

  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

  x:Name="m_TestIcon"
  Height="32" Width="32"
>
  <UserControl.Resources>
    <Style TargetType="Image">
      <Setter Property="Width" Value="32" />
      <Setter Property="Height" Value="32" />
    </Style>
  </UserControl.Resources>

  <Image
    x:Name="m_TestIcon_Image"
  />

</UserControl>

Here is the code-behind:

   public partial class TestIcon : UserControl
    {
        public TestIcon()
        {
            InitializeComponent();
            m_TestIcon_Image.Source = createBitmapImageFromResource("/Resources/Images/TestIcon32.png", 32);
        }

        private static BitmapImage createBitmapImageFromResource(string resource_name, int icon_width)
        {
            // Create source
            BitmapImage myBitmapImage = new BitmapImage();

            // BitmapImage.UriSource must be in a BeginInit/EndInit block
            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri(resource_name, UriKind.Relative);

            // To save significant application memory, set the DecodePixelWidth or 
            // DecodePixelHeight of the BitmapImage value of the image source to the desired
            // height or width of the rendered image. If you don't do this, the application will
            // cache the image as though it were rendered as its normal size rather then just
            // the size that is displayed.
            // Note: In order to preserve aspect ratio, set DecodePixelWidth
            // or DecodePixelHeight but not both.
            myBitmapImage.DecodePixelWidth = icon_width;
            myBitmapImage.EndInit();

            return myBitmapImage;
        }
    }

Solution

The answer was given in a link contributed by Tri Q below. I changed the last line of the constructor to the following line and it worked:

m_ViewIconUC_Image.Source = new BitmapImage(new Uri(@"/MyAssembly;component/Resources/Images/TestIcon32.png", UriKind.Relative));
+1  A: 

Try adding a DependencyProperty of type BitmapImage to your UserControl and bind the image control to it. By registering the DP specify its default value.

Dmytro Laptin
+1  A: 

Have a look here.

Tri Q
Thanks Tri Q - that worked. The comments don't format code well, so I put the solution in an edit to the question above.
Brian Stewart