none
add click in image with wpf

    问题

  • Good morning

    I want to add click event to image tool ,after I appear it in form 
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                String x;
    
                x = "D:\\Users\\aisha\\Desktop\\010.png";
                
                ImageSource imageSource = new BitmapImage(new Uri(x));
    
                image1.Source = imageSource;
                 
    
            }
     I work wpf under c#
    2012年4月15日 19:22

答案

  • You are not getting it.  WPF allows you to create your own button.  Buttons have behaviours like Click.  While you can use other events you will not always get the expected behaviour.  I would suggest reading a book about WPF and listening to the people who post here.  When I started a couple of years ago I came from a WinForms background and the WPF learning curve can be long.

    If you want a certain behaviour you start with the control that has that behaviour and modify it to suit your means.  

    I also notice that you are using the old method of code behind filling controls.  I would suggest reading up on Databinding in WPF.  If you don't use the things that WPF allows you to do with little effort you will eventually find that using WPF is not what you want and that WinForms is easier.  When I first started I had no patterns to help with how to use WPF.  Listen to this forum (read all the posts, they always have useful information) and you will get it, just won't happen in a day.

    I use the following style to get a "button" with no borders that will act on a click event.

        <Style x:Key="NoButtonBorder" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <ContentPresenter                  
                                Margin="{TemplateBinding Padding}"                  
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                  
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                  
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"                  
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                RecognizesAccessKey="True"                  
                                Content="{TemplateBinding Content}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    Hope this helps

    LS


    Lloyd Sheen

    2012年4月16日 22:21

全部回复

  • plz quickly ..

    Thanks ,,Regards
    2012年4月15日 19:23
  • You can use the MouseUp event instead of the click event.

    Image image = new Image();
                image.MouseUp += (s, e) => 
                {
                    //Insert code here.
                };


    Developing is part of being a developer.

    2012年4月15日 19:33
  • Error 1 A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else .

    this error appear,,,?

    2012年4月15日 19:50
  • aishaa,

    Try giving some other name instead of 'e'.

    'e' is already defined in your code 'button1_Click(object sender, RoutedEventArgs e)'

    Thanks,

    IamHuM

    2012年4月15日 20:27
  • Ah, my bad, try like this.

    Image image = new Image();
                image.MouseUp += (ss, ee) => 
                {
                    //Insert code here.
                };

    
    
    

    Developing is part of being a developer.

    2012年4月15日 20:50
  • That's not work , I am so sorry!

    no thing appear when i press run.
    • 已编辑 aishaaa 2012年4月15日 22:14
    2012年4月15日 22:06
  • Can you show how your code is currently looking?

    Developing is part of being a developer.

    2012年4月15日 22:40
  • string[] ltr = new string[] { "D:\\010.png",_

    "D:\\020.png", "D:\\030.png",_

    "D:\\040.png" }; private void button1_Click(object sender, RoutedEventArgs e) { ImageSource a = new BitmapImage(new Uri(pic_ltr[0])); ImageSource b = new BitmapImage(new Uri(pic_ltr[1])); ImageSource c = new BitmapImage(new Uri(pic_ltr[2])); ImageSource d = new BitmapImage(new Uri(pic_ltr[3])); image2.Source = a; image1.Source = b; image3.Source = c; image4.Source = d;



    • 已编辑 aishaaa 2012年4月15日 23:14 edit som path in code
    2012年4月15日 22:54
  • and I don't know how to make path short ( file image name, image name)

    Now I should write full path for run my project.

    THANKS .REGARDS

    2012年4月15日 23:22
  • I would advise you to put your images in a list. If you do that you could do like this.

    List<Image> Images = new List<Image>();
                string[] ltr = new string[] 
                {
                     "D:\\010.png",
                     "D:\\020.png", 
                     "D:\\030.png",
                     "D:\\040.png"
                };
    
                for (int i = 0; i < ltr.Length; ++i)
                {
                    ImageSource ims = new BitmapImage(new Uri(ltr[i]));
                    Image im = new Image();
                    im.Source = ims;
                    im.MouseUp += (ss, ee) => 
                    {
                        MessageBox.Show("You clicked on this image.");
                    };
    
    
                    Images.Add(im);
                } 

    You will have less to keep track of, and simpler code to deal with.


    Developing is part of being a developer.

    2012年4月16日 7:23
  • Thanks for your advice ,your code is nice but no thing appear when I the run time 

    the code work with out error and with out results.

    please try it in wpf and tell me what happen.

    Thank you alot for your time 

       Regards
    2012年4月16日 18:25
  • If you could share the current code that you have based on the sample I provided, I might be capable of giving you better advise.

    Developing is part of being a developer.

    2012年4月16日 20:05
  • I use your code without change any thing except the path of image I write the full path .

    (aisha.mohd)

    this my skype name if you want use share screen or teamviewer.

    Thanks, with my best regards


    • 已编辑 aishaaa 2012年4月16日 21:21
    2012年4月16日 21:20
  • One of the cool things in WPF is its ability to create compound GUI objects.  You can use this to create a button with an image (or just about what ever you want as the content of the button).

    Example:

    <Button>
       <StackPanel>
          <TextBlock>Click Me!</TextBlock>
          <Image ImageSource="path to image"></Image>
       </StackPanel>
    </Button>

    You can then attach a Click event handler as the above button will handle the click if you click anywhere within the "Button".  With styling you can make the button look exactly like you want.

    Hope this helps,

    Lloyd Sheen


    Lloyd Sheen

    2012年4月16日 21:53
  • no not that , I want image has click event not button 
    2012年4月16日 22:10
  • You are not getting it.  WPF allows you to create your own button.  Buttons have behaviours like Click.  While you can use other events you will not always get the expected behaviour.  I would suggest reading a book about WPF and listening to the people who post here.  When I started a couple of years ago I came from a WinForms background and the WPF learning curve can be long.

    If you want a certain behaviour you start with the control that has that behaviour and modify it to suit your means.  

    I also notice that you are using the old method of code behind filling controls.  I would suggest reading up on Databinding in WPF.  If you don't use the things that WPF allows you to do with little effort you will eventually find that using WPF is not what you want and that WinForms is easier.  When I first started I had no patterns to help with how to use WPF.  Listen to this forum (read all the posts, they always have useful information) and you will get it, just won't happen in a day.

    I use the following style to get a "button" with no borders that will act on a click event.

        <Style x:Key="NoButtonBorder" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <ContentPresenter                  
                                Margin="{TemplateBinding Padding}"                  
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                  
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                  
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"                  
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                RecognizesAccessKey="True"                  
                                Content="{TemplateBinding Content}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    Hope this helps

    LS


    Lloyd Sheen

    2012年4月16日 22:21
  • Thanks for your advice , ,

    I am should work from #c code not with xaml ,I load the xaml code from blend .

    Thanks again ,,for your time

    2012年4月16日 23:24