add click in image with wpf
-
15 Nisan 2012 Pazar 19:22
Good morning
I want to add click event to image tool ,after I appear it in formprivate 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#
Tüm Yanıtlar
-
15 Nisan 2012 Pazar 19:23plz quickly ..
Thanks ,,Regards -
15 Nisan 2012 Pazar 19:33
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.
-
15 Nisan 2012 Pazar 19:50
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,,,? -
15 Nisan 2012 Pazar 20:27
aishaa,
Try giving some other name instead of 'e'.
'e' is already defined in your code 'button1_Click(object sender, RoutedEventArgs e)'
Thanks,
IamHuM
-
15 Nisan 2012 Pazar 20:50
Ah, my bad, try like this.
Image image = new Image(); image.MouseUp += (ss, ee) => { //Insert code here. };
Developing is part of being a developer.
-
15 Nisan 2012 Pazar 22:06That's not work , I am so sorry!
no thing appear when i press run.- Düzenleyen aishaaa 15 Nisan 2012 Pazar 22:14
-
15 Nisan 2012 Pazar 22:40Can you show how your code is currently looking?
Developing is part of being a developer.
-
15 Nisan 2012 Pazar 22:54
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;
- Düzenleyen aishaaa 15 Nisan 2012 Pazar 23:14 edit som path in code
-
15 Nisan 2012 Pazar 23:22and 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
-
16 Nisan 2012 Pazartesi 07:23
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.
-
16 Nisan 2012 Pazartesi 18:25Thanks 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 -
16 Nisan 2012 Pazartesi 20:05If 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.
-
16 Nisan 2012 Pazartesi 21:20
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
- Düzenleyen aishaaa 16 Nisan 2012 Pazartesi 21:21
-
16 Nisan 2012 Pazartesi 21:53
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
-
16 Nisan 2012 Pazartesi 22:10no not that , I want image has click event not button
-
16 Nisan 2012 Pazartesi 22:21
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
- Yanıt Olarak İşaretleyen Kee PoppyModerator 23 Nisan 2012 Pazartesi 03:19
-
16 Nisan 2012 Pazartesi 23:24
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