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. WPF
  4. Referencing DrawingImage in a binding.

Referencing DrawingImage in a binding.

Scheduled Pinned Locked Moved WPF
questionwpfwcfgraphicsdevops
4 Posts 2 Posters 13 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.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    I'm not certain what I have to google for. I have Drawing images in an resource file like this : I can use them like this : Now, I want to be able to bind the image source and conditionally choose the image in the Code-Behind. I'm not certain how I reference the image in the code ? Is this a DrawingImage ? How do I load it in the code ? public DrawingImage ChooseIcon { get { if (useFindIcon) { return ???; // how to reference the static resource "FindIcon" } else if (useSearchIcon) { return ???;// how to reference the static resource "SearchIcon" } } } Thanks. Max.

    CI/CD = Continuous Impediment/Continuous Despair

    Richard DeemingR 1 Reply Last reply
    0
    • M Maximilien

      I'm not certain what I have to google for. I have Drawing images in an resource file like this : I can use them like this : Now, I want to be able to bind the image source and conditionally choose the image in the Code-Behind. I'm not certain how I reference the image in the code ? Is this a DrawingImage ? How do I load it in the code ? public DrawingImage ChooseIcon { get { if (useFindIcon) { return ???; // how to reference the static resource "FindIcon" } else if (useSearchIcon) { return ???;// how to reference the static resource "SearchIcon" } } } Thanks. Max.

      CI/CD = Continuous Impediment/Continuous Despair

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The view-model shouldn't really know anything about your resources. I'd suggest having an enum to represent the icon you want to display. Bind the source to that enum, and use a converter to convert the value to the relevant image. Eg:

      public enum ChooseIcon
      {
      Find,
      Search,
      }

      public class YourViewModel
      {
      private ChooseIcon _chooseIcon;

      public ChooseIcon ChooseIcon
      {
          get { return \_chooseIcon; }
          set { SetProperty(ref \_chooseIcon, value); }
      }
      

      }

      public class ChooseIconConverter : IValueConverter
      {
      public ImageSource FindIcon { get; set; }
      public ImageSource SearchIcon { get; set; }

      public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          switch ((ChooseIcon)value)
          {
              case ChooseIcon.Find: return FindIcon;
              case ChooseIcon.Search: return SearchIcon;
              default: return null;
          }
      }
      
      public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          return Binding.DoNothing;
      }
      

      }

      <Window.Resources>
      <local:ChooseIconConverter
      x:Key="ChooseIconConverter"
      FindIcon="{StaticResource FindIcon}"
      SearchIcon="{StaticResource SearchIcon}"
      />
      </Window.Resources>
      ...
      <Image Source="{Binding ChooseIcon, Mode=OneWay, Converter={StaticResource ChooseIconConverter}}" />


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        The view-model shouldn't really know anything about your resources. I'd suggest having an enum to represent the icon you want to display. Bind the source to that enum, and use a converter to convert the value to the relevant image. Eg:

        public enum ChooseIcon
        {
        Find,
        Search,
        }

        public class YourViewModel
        {
        private ChooseIcon _chooseIcon;

        public ChooseIcon ChooseIcon
        {
            get { return \_chooseIcon; }
            set { SetProperty(ref \_chooseIcon, value); }
        }
        

        }

        public class ChooseIconConverter : IValueConverter
        {
        public ImageSource FindIcon { get; set; }
        public ImageSource SearchIcon { get; set; }

        public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((ChooseIcon)value)
            {
                case ChooseIcon.Find: return FindIcon;
                case ChooseIcon.Search: return SearchIcon;
                default: return null;
            }
        }
        
        public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Binding.DoNothing;
        }
        

        }

        <Window.Resources>
        <local:ChooseIconConverter
        x:Key="ChooseIconConverter"
        FindIcon="{StaticResource FindIcon}"
        SearchIcon="{StaticResource SearchIcon}"
        />
        </Window.Resources>
        ...
        <Image Source="{Binding ChooseIcon, Mode=OneWay, Converter={StaticResource ChooseIconConverter}}" />


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        Thanks. Will look at that. I also bodge a hack by having 2 Image tags and add a Visibility binding.

        CI/CD = Continuous Impediment/Continuous Despair

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          The view-model shouldn't really know anything about your resources. I'd suggest having an enum to represent the icon you want to display. Bind the source to that enum, and use a converter to convert the value to the relevant image. Eg:

          public enum ChooseIcon
          {
          Find,
          Search,
          }

          public class YourViewModel
          {
          private ChooseIcon _chooseIcon;

          public ChooseIcon ChooseIcon
          {
              get { return \_chooseIcon; }
              set { SetProperty(ref \_chooseIcon, value); }
          }
          

          }

          public class ChooseIconConverter : IValueConverter
          {
          public ImageSource FindIcon { get; set; }
          public ImageSource SearchIcon { get; set; }

          public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              switch ((ChooseIcon)value)
              {
                  case ChooseIcon.Find: return FindIcon;
                  case ChooseIcon.Search: return SearchIcon;
                  default: return null;
              }
          }
          
          public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              return Binding.DoNothing;
          }
          

          }

          <Window.Resources>
          <local:ChooseIconConverter
          x:Key="ChooseIconConverter"
          FindIcon="{StaticResource FindIcon}"
          SearchIcon="{StaticResource SearchIcon}"
          />
          </Window.Resources>
          ...
          <Image Source="{Binding ChooseIcon, Mode=OneWay, Converter={StaticResource ChooseIconConverter}}" />


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          Awesome, it's working. :rose:

          CI/CD = Continuous Impediment/Continuous Despair

          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