Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Trouble with custom MarkupExtension [modified]

Trouble with custom MarkupExtension [modified]

Scheduled Pinned Locked Moved WCF and WF
wpfhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chaiguy1337
    wrote on last edited by
    #1

    I am annoyed at having to declare icons for a menu item as static resources and then reference those resources in my menu definition, so I came up with the idea of creating a MarkupExtension called ImageLoader that inputs a relative Source (and height and width properties) and returns a fully-instantiated Image object so I can reference these inline in my menu definition:

    <MenuItem
    Header="Options..."
    Icon="{util:ImageLoader /Resources/Icons/pi_gear_16.png, Height=16, Width=16}" />

    (Before I go any further, if you know of a better/easier way to do this, please tell me!) However, I'm running into an odd problem inside my MarkupExtension definition where when I pass the path into ImageSourceConverter.ConvertFrom(), it interprets it as a file path (of the default drive) rather than a resource path. I say it's odd because when I create an Image directly in XAML, I am passing the exact same string into its ImageSource attribute, and it works fine! So why is it interpreting it as a file when I do it manually?

    [MarkupExtensionReturnType( typeof( Image ) )]
    public class ImageLoaderExtension: MarkupExtension {

    public Double Width { get; set; }
    public Double Height { get; set; }
    
    string imagePath;
    
    public ImageLoaderExtension( string imagePath ) {
    	this.imagePath = imagePath;
    }
    
    public override object ProvideValue( IServiceProvider serviceProvider ) {
    	Image image = new Image();
    	image.Source = (ImageSource)( new ImageSourceConverter().ConvertFrom( imagePath ) );
    	image.Height = this.Height;
    	image.Width = this.Width;
    	return image;
    }
    

    }

    Thanks for any input. -Logan

    modified on Thursday, September 4, 2008 7:39 PM

    W 1 Reply Last reply
    0
    • C chaiguy1337

      I am annoyed at having to declare icons for a menu item as static resources and then reference those resources in my menu definition, so I came up with the idea of creating a MarkupExtension called ImageLoader that inputs a relative Source (and height and width properties) and returns a fully-instantiated Image object so I can reference these inline in my menu definition:

      <MenuItem
      Header="Options..."
      Icon="{util:ImageLoader /Resources/Icons/pi_gear_16.png, Height=16, Width=16}" />

      (Before I go any further, if you know of a better/easier way to do this, please tell me!) However, I'm running into an odd problem inside my MarkupExtension definition where when I pass the path into ImageSourceConverter.ConvertFrom(), it interprets it as a file path (of the default drive) rather than a resource path. I say it's odd because when I create an Image directly in XAML, I am passing the exact same string into its ImageSource attribute, and it works fine! So why is it interpreting it as a file when I do it manually?

      [MarkupExtensionReturnType( typeof( Image ) )]
      public class ImageLoaderExtension: MarkupExtension {

      public Double Width { get; set; }
      public Double Height { get; set; }
      
      string imagePath;
      
      public ImageLoaderExtension( string imagePath ) {
      	this.imagePath = imagePath;
      }
      
      public override object ProvideValue( IServiceProvider serviceProvider ) {
      	Image image = new Image();
      	image.Source = (ImageSource)( new ImageSourceConverter().ConvertFrom( imagePath ) );
      	image.Height = this.Height;
      	image.Width = this.Width;
      	return image;
      }
      

      }

      Thanks for any input. -Logan

      modified on Thursday, September 4, 2008 7:39 PM

      W Offline
      W Offline
      Walt Dell
      wrote on last edited by
      #2

      The following may work for you (it assumes that you want a BitmapImage):

      image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(imagePath, UriKind.Relative));

      -Walt

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups