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. WPF Button Image Source

WPF Button Image Source

Scheduled Pinned Locked Moved WPF
csharpwpfhelpquestion
4 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.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I created a button class:

    public class ImageTextButton : Button
    {
    public ImageSource NormalImageSource
    {
    get { return (ImageSource)GetValue(NormalImageSourceProperty); }
    set { SetValue(NormalImageSourceProperty, value); }
    }
    public static readonly DependencyProperty NormalImageSourceProperty =
    DependencyProperty.Register("NormalImageSource", typeof(ImageSource), typeof(ImageTextButton), new UIPropertyMetadata(null));

    public ImageSource MouseOverImageSource
    {
        get { return (ImageSource)GetValue(MouseOverImageSourceProperty); }
        set { SetValue(MouseOverImageSourceProperty, value); }
    }
    public static readonly DependencyProperty MouseOverImageSourceProperty =
        DependencyProperty.Register("MouseOverImageSource", typeof(ImageSource), typeof(ImageTextButton), new UIPropertyMetadata(null));
    
    
    public string Caption
    {
        get { return (string)GetValue(CaptionProperty); }
        set { SetValue(CaptionProperty, value); }
    }
    public static readonly DependencyProperty CaptionProperty =
        DependencyProperty.Register("Caption", typeof(string), typeof(ImageTextButton), new UIPropertyMetadata(null));
    

    }

    and I use it like this:

    In the button's ControlTemplate I'm doing:

    but it doesn't work. The image doesn't change. It disappears. Anyone know the right way to do this? Thanks

    If it's not broken, fix it until it is

    U 1 Reply Last reply
    0
    • K Kevin Marois

      I created a button class:

      public class ImageTextButton : Button
      {
      public ImageSource NormalImageSource
      {
      get { return (ImageSource)GetValue(NormalImageSourceProperty); }
      set { SetValue(NormalImageSourceProperty, value); }
      }
      public static readonly DependencyProperty NormalImageSourceProperty =
      DependencyProperty.Register("NormalImageSource", typeof(ImageSource), typeof(ImageTextButton), new UIPropertyMetadata(null));

      public ImageSource MouseOverImageSource
      {
          get { return (ImageSource)GetValue(MouseOverImageSourceProperty); }
          set { SetValue(MouseOverImageSourceProperty, value); }
      }
      public static readonly DependencyProperty MouseOverImageSourceProperty =
          DependencyProperty.Register("MouseOverImageSource", typeof(ImageSource), typeof(ImageTextButton), new UIPropertyMetadata(null));
      
      
      public string Caption
      {
          get { return (string)GetValue(CaptionProperty); }
          set { SetValue(CaptionProperty, value); }
      }
      public static readonly DependencyProperty CaptionProperty =
          DependencyProperty.Register("Caption", typeof(string), typeof(ImageTextButton), new UIPropertyMetadata(null));
      

      }

      and I use it like this:

      In the button's ControlTemplate I'm doing:

      but it doesn't work. The image doesn't change. It disappears. Anyone know the right way to do this? Thanks

      If it's not broken, fix it until it is

      U Offline
      U Offline
      Unnikrishnan_S_N
      wrote on last edited by
      #2

      Hi Kevin, Just updating the source won't work. You have to update the Button Content. for ex: <BitmapImage UriSource="{Binding RelativeSource={RelativeSource Self}, Path=MouseOverImageSource}" />

      <ControlTemplate.Triggers>
      <Trigger Property="Button.IsMouseOver" Value="True">
      <Setter TargetName="ButtonImage" Property="Content"/> <Setter.Value>
      <StackPanel>
      <BitmapImage UriSource="{Binding RelativeSource={RelativeSource Self}, Path=MouseOverImageSource}" />

      <Setter.Value/>
      </Trigger>
      </ControlTemplate.Triggers>

      I think this should work..

      K 1 Reply Last reply
      0
      • U Unnikrishnan_S_N

        Hi Kevin, Just updating the source won't work. You have to update the Button Content. for ex: <BitmapImage UriSource="{Binding RelativeSource={RelativeSource Self}, Path=MouseOverImageSource}" />

        <ControlTemplate.Triggers>
        <Trigger Property="Button.IsMouseOver" Value="True">
        <Setter TargetName="ButtonImage" Property="Content"/> <Setter.Value>
        <StackPanel>
        <BitmapImage UriSource="{Binding RelativeSource={RelativeSource Self}, Path=MouseOverImageSource}" />

        <Setter.Value/>
        </Trigger>
        </ControlTemplate.Triggers>

        I think this should work..

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Thanks

        virusattack wrote:

        <Button>
        < Button.Content>

        < Image>
        < Image.Source>
        < BitmapImage UriSource="{Binding RelativeSource={RelativeSource Self}, Path=MouseOverImageSource}" />
        < /Image.Source>
        < /Image>

        < /Button.Content>

        Where does this code go in the style?

        If it's not broken, fix it until it is

        U 1 Reply Last reply
        0
        • K Kevin Marois

          Thanks

          virusattack wrote:

          <Button>
          < Button.Content>

          < Image>
          < Image.Source>
          < BitmapImage UriSource="{Binding RelativeSource={RelativeSource Self}, Path=MouseOverImageSource}" />
          < /Image.Source>
          < /Image>

          < /Button.Content>

          Where does this code go in the style?

          If it's not broken, fix it until it is

          U Offline
          U Offline
          Unnikrishnan_S_N
          wrote on last edited by
          #4

          Hi Kevin, check in Trigger whether Button's IsMouseOver is true and set the property Content with the following values.

          <ControlTemplate.Triggers>
          <Trigger Property="Button.IsMouseOver" Value="True">
          <Setter TargetName="ButtonImage" Property="Content"/> <Setter.Value>
          <StackPanel>
          <Image>
          <Image.Source>
          <BitmapImage UriSource="{Binding RelativeSource={RelativeSource Self}, Path=MouseOverImageSource}" />
          </Image.Source>
          </Image>
          </StackPanel>
          <Setter.Value/>
          </Trigger>
          </ControlTemplate.Triggers>

          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