I'm generating a list of image controls in WPF .
Image img = new Image();
img.Source = GetPic(value);
img.Tag = cst.GetIdDriverde();
img.ToolTip = dtID.Rows[j][0];
img.Width = 130;
img.Height = 130;
lst.Add(img);
I want to add an event to each control.
How can I do that?
I agree with Mighty and Omid but if you run stubborn, here's a lambda
img.MouseLeftButtonUp += (se, a) => {/*YOUR CODE*/};
You could attach an event like you do it for any other object.
For example a MouseLeftButtonDown event.
Image img = new Image();
img.MouseLeftButtonDown += Img_MouseLeftButtonDown;
private void Img_MouseLeftButtonDown(object sender, MouseButtonEventArgs eventArgs)
{
//your logic
}
Have a look at the Image Class for the available events.
Hint
This would work but you should not add images like this at all. The better way would be to use a ItemsControl and using a DataTemplate. If you want to change your code let me know if you need some help.
Related
I am trying to move a rectangle when user drags it. My code is:
private void Grid_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
{
Rectangle r = (sender) as Rectangle;
r.RenderTransform = new CompositeTransform { TranslateX = e.DeltaManipulation.Translation.X };
}
Above code doesn't work because it should be like this:
r.RenderTransform = new CompositeTransform { TranslateX += e.DeltaManipulation.Translation.X };
"+" sign makes it work but above syntax give an error. It works fine when I add Render transform using xaml but I want to do it in c#. Any suggestion?
Just putting the solution proposed in the comments into an answer:
They way you did it was to assign a new CompositeTransform on every call of the event handler. This is not what you want. You want to manipulate an existing CompositeTransform instead.
In order to achieve this, you have to manually assign the CompositeTransform once outside your eventhandler:
r.RenderTransform = new CompositeTransform();
After this you can use the following in your event handler:
((CompositeTransform)r.RenderTransform).TranslateX += e.DeltaManipulation.Translation.X;
Solved: Using design toolbox.
I'm using the following method to add a image
private void cobertura1vertente_Load(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.Image = Image.FromFile("C:/cobertura1vertente.png");
pb1.Location = new Point(3, 3);
pb1.Size = new Size(250, 250);
//doubt right below
groupBox81.Controls.Add(pb1);
}
I usually would add a image to a entire form, but now i want to add it to a specific groupbox, who lies inside a tab.
If the code is correct, my guess is that the location that i've set is relative to the whole form, and not to the groupBox, why would that happen?
How to add a parameter for an image to this variable?
var selectionDisplay = new SelectionDisplay(button.Label as string);
Thanks in advance!
EDIT:
I have this set of images and their respective code below (see pic1)
This is where the program gets the images to be displayed. The code for the buttons is this one:
var files = Directory.GetFiles(#".\GalleryImages");
foreach (var file in files)
{
FileInfo fileInfo = new FileInfo(file);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(file, UriKind.Relative);
bi.EndInit();
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi)
};
this.wrapPanel.Children.Add(button);
}
This is where the program gets the images to be displayed.
The code for the buttons is this one:
private void KinectTileButtonclick(object sender, RoutedEventArgs e)
{
var button = (KinectTileButton)e.fake_fake_fakeource;
var selectionDisplay = new SelectionDisplay(button.Label as string);
this.kinectRegionGrid.Children.Add(selectionDisplay);
e.Handled = true;
Right now, when i click on of the images, the SelectionDisplay window pops up, which look like this (see pic2). What i want is that when I click an image the SelectionDisplay window should open with the respective image... meaning that if I click on the image with a dog, the window should open with the dog's image, not with other image.
I hope I've made myself clear and that you can help me.
http://i58.tinypic.com/8zl6h3.jpg
http://i57.tinypic.com/208fosy.png
is this the constructor you are talking about? is this where i should make changes? should i add something after "string itemid"?
public SelectionDisplay(string itemId)
{
this.InitializeComponent();
this.messageTextBlock.Text = string.Format(CultureInfo.CurrentCulture,Properties.Resources.SelectedMessage,itemId);
}
I see two approaches:
Just pass the image brush in your constructor. Its view->view, so you aren't breaking MVVM (and it looks like you aren't using that pattern anyways).
new SelectionDisplay(button.Label, button.Background);
Set the path as the "tag" of the button. The tag property is an object you can put whatever you want into (the framework does not use it, and so it is included for this very purpose). Then just pass the string to SelectionDisplay, and instantiate the image just like you are doing for the button:
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi),
Tag = file
};
var selectionDisplay = new SelectionDisplay(button.Label as string, button.Tag as string);
FrameworkElement.Tag on MSDN (Note that Button derives from FrameworkElement, as do all controls, so it automatically has it as well!)
UPDATE
I see that SelectionDisplay is a UserControl in your project, so you just need to change its constructor to look like:
Numbers match above:
SelectionDisplay(string labelText, ImageBrush sourceImage)
SelectionDisplay(string labelText, string imagePath)
That is the source of the error you are getting, you have to modify the constructor to take the new parameter.
Please let me know if I can clarify anything.
I want to iterate through all the files in a folder and dynamically create images controls for each JPEG file found. Once complete I want a form filled with dynamically created image controls (think of just about any photo viewing software such as Picasa that has a thumbnail view).
I want to be able to reorder these dynamically created images controls on the form by implementing some sort of drag drop event handler. I will not know how many images I will encounter and therefore cannot hardcode event handlers for each image control that might or might not exist. So I am looking for a way to dynamically add event handlers to dynamically created controls.
The method used in the code below is almost what I am looking for. The problem with the method below is that if I don't know the name of the control I could not hard code the event handler.
public partial class RoutedEventAddRemoveHandler {
void MakeButton(object sender, RoutedEventArgs e)
{
Button b2 = new Button();
b2.Content = "New Button";
// Associate event handler to the button. You can remove the event
// handler using "-=" syntax rather than "+=".
b2.Click += new RoutedEventHandler(Onb2Click);
root.Children.Insert(root.Children.Count, b2);
DockPanel.SetDock(b2, Dock.Top);
text1.Text = "Now click the second button...";
b1.IsEnabled = false;
}
void Onb2Click(object sender, RoutedEventArgs e)
{
text1.Text = "New Button (b2) Was Clicked!!";
}
}
Note I am looking for a solution in c# code not XAML. That is a solution using code like this to add controls:
// What I want
Fields.Add(new Field() { Name = "Username", Length = 100, Required = true });
not like this:
// What I do not want
<TextBox Width="100" Canvas.Left="50" Canvas.Top="20" />
Thanks
I would not do so much in codebehind. Only to get the files.
I would get an ObservableCollection where the string is the FullName of the file.
Then I would present it in a ListBox or ListView having the ItemSource bound to the collection and defining å good ItemTemplate for the control.
In the template, you can use a Converter to create å Source for the Image in the template.
Adding a small sample just to save you the pain of image loading in WPF code-behind.
void OnButtonClick(object sender, RoutedEventArgs routedEventArgs)
{
var files = Directory.GetFiles(#"C:\img");
foreach (var file in files)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(file);
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
var img = new Image { Source = bitmap };
img.MouseDown += OnImageMouseDown;
//Add img to your container
}
}
void OnImageMouseDown(object sender, MouseButtonEventArgs e)
{
var img = sender as Image;
//Operate
}
I have an mdichild, and an eventhandler for draganddrop so when I drop an image file in my form, a picturebox ( name = dpic ) is created with that image.
I have another eventhandler which is for dpic_Click, so when I click on the image, my form's text is the name of that image.
After the first time I drop an image, another dpic is created, because you can't have two cotrols with the same name. C# automatically changes the name and that makes my event handlers only work for the last image I dropped in. I think if I could make an event handler for my mdichild that gets the name of the control that is under the mouse pointer I could simply change back the image I am pointing at.
UPDATE
Here is my code , I made an event handler for droping in my mdichild :
void mdiChild_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
dpic = new PictureBox() ;
string[] filepath = (string[])e.Data.GetData(DataFormats.FileDrop);
Image image = Image.FromFile(filepath[0]);
dpic.Image = image;
dpic.Tag = Path.GetFileName(filepath[0]);
this.ActiveMdiChild.Controls.Add(dpic);
dpic.ContextMenuStrip = this.contextMenuStrip1;
this.ActiveMdiChild.Refresh();
dpic.BringToFront();
this.ActiveMdiChild.ActiveControl = dpic;
dpic.Click += new EventHandler(dpic_Click);
// _____this helped me do it_________________________________________________
foreach (Control c in this.ActiveMdiChild.Controls)
{
c.Click += new EventHandler(c_Click);
}
// ________________________________________________________________________
}
}
What you are looking for is a sender. The sender will tell which image was clicked and will allow you to get its name.
PictureBox picSender = (PictureBox)sender;
label1.Text = picSender.Name;
EDIT : You put that in the pic_Click event
I don't understand exactly what you want to do here, possibly layering new pictureboxes with dropped images on the form?
If that is so you can use a
List<PictureBox> picboxes = new List<PictureBox>();
and where you do:
dpic = new PictureBox() ;
change to
PictureBox dpic = new PictureBox() ;
picboxes.Add(dpic);
this.Controls.Add(dpic);
But please note that you cannot have unlimited controls declared.