Dynamically created button in WPF - c#

I'm try to do a bit of WPF, only really done windows forms until now and not a lot of that...
All I'm trying to do is to dynamically within code (not xaml) set a button to show an image and set the size of the button to auto size to the image.
The code below loads the image but it goes when the mouse is over the button and the button doesn't auto size to the image.
tbButtonPicture contains a local path on the PC to a bitmap e.g. C:\temp\my Artwork\test1.bmp
This what I have thus far which sits inside a loop:
Console.WriteLine(tbButtonPicture);
System.Windows.Controls.Button newBtn = new Button();
//newBtn.Content = i.ToString();
newBtn.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), tbButtonPicture)));
newBtn.Name = "Button" + i.ToString();
sp.Children.Add(newBtn);
i++;

Wrap your image in an Image control and set this as the button content and you should have your desired effect.
System.Windows.Controls.Button newBtn = new Button();
Image imageControl = new Image();
imageControl.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), tbButtonPicture));
newBtn.Content = imageControl;
newBtn.Name = "Button" + i.ToString();
sp.Children.Add(newBtn);
i++;
But I totally agree with above comments:
try to solve your issues in xaml its much more easier. Read the suggested resources, they are really helpful.

Related

How do I display a PictureBox on a PictureBox in C#?

I want to display a PictureBox ontop of a Picturebox. I have a "mother" Picturebox and a button next to it. Everytime you click the Button a new PictureBox should be displayed on the "mother". I have created the PictureBox like this:
PictureBox newPictureBox = new PictureBox();
newPictureBox.Location = new Point(x:30,y:30);
newPictureBox.BackColor = Color.Red;
newPictureBox.Visible = true;
newPictureBox.Height = 200;
newPictureBox.Width = 200;
Now I have no idea how to display it to the user. I tried to use .Show() and
Call the GetChildIndex and SetChildIndex methods of the parent's Controls collection.
I also tried that. Either I don't know how to call it or it just doesn't work. Been searching for a solution for way too long. Does anybody have an idea of how to display that PictureBox on top of that pictureBox?
So turns out, I forgot to add the picture Box to the "mother" Picturebox. I added 1 line:
motherPictureBox.Controls.Add(newPictureBox);
So now my Code looks like this:
PictureBox newPictureBox = new PictureBox();
newPictureBox.Location = new Point(x:30,y:30);
newPictureBox.BackColor = Color.Red;
newPictureBox.Visible = true;
newPictureBox.Height = 200;
newPictureBox.Width = 200;
motherPictureBox.Controls.Add(newPictureBox);
Didn't need to bring it forward, I just forgot to add it...
you need to add new picturebox to form's controls
PictureBox NewPictureBox = new PictureBox();
NewPictureBox.BackColor = Color.Red;
NewPictureBox.Location = MotherPictureBox.Location;
NewPictureBox.Size = MotherPictureBox.Size;
this.Controls.Add(NewPictureBox);
NewPictureBox.BringToFront();

Adding image to a button in WPF C#, image not appearing

I want to create a button in my windows 8 desktop app, which will have an image and a text block. I have to do this using C# coding.
My code is as follows,
Button btn = new Button();
StackPanel btnContentPanel = new StackPanel();
btnContentPanel.Orientation = Orientation.Horizontal;
Image img = new Image();
img.Source = new BitmapImage(newUri(#"C:\Users\Desktop\Images\download.jpg"));
img.Stretch = Stretch.Uniform;
btnContentPanel.Children.Add(img);
TextBlock txBlock = new TextBlock();
txBlock.Text = "My Button";
btnContentPanel.Children.Add(txBlock);
btn.Content = btnContentPanel;
This is not giving any error but the image is not getting displayed. If I add another text block in place of the image, then its appearing, but not the image.
Am I missing anything ? Please help, thank you.
Try building your button like this:
Button btn= new Button
{
Width = 30,
Height = 30,
Content = new Image
{
Source = new BitmapImage(#"C:\Users\Desktop\Images\download.jpg"))
}
};
In the case of a 'missing' image there are several things to consider:
When Xaml can't locate a resource it might ignore it (when it won't throw a XamlParseException)
The Resource must be properly added and defined:
make sure it exists in your project where expected.
Make sure it is built with your project as a Resource.
(Right click -> Properties -> BuildAction='Resource')
Another thing to try in similar cases, which is also useful for reusing of the image (or any other resource):
Define your Image as a Resource in your Xaml:
<UserCondrol.Resources>
<Image x:Key="MyImage" Source.../>
</UserControl.Resources>
And later use it in your desired control/controls:
<Button Content={StaticResource MyImage} />

Adding Image to wpf

I want to add an Image dynamically to the wpf application.
What I have tried :
Image img = new Image();
img.Height = 100;
img.Width = 100;
img.Visibility = Visibility.Visible;
Thickness th = new Thickness(100, 100, 0, 0);
img.Margin = th;
string strUri2 = String.Format(#"pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
img.Source = new BitmapImage(new Uri(strUri2));
I know, the image won't display unless I add it to the Content.
this.Content=img;
but with this the existing controls(shapes) on the app are lost.
Now, my question is how to add the image to the content without losing existing controls from the app.
When you are going to load and show an image dynamically, you still need to think about the layout of your application. I would suggest to add an (initially empty) Image control in XAML (for example in a Grid), and later set the Source property of this control in code.
<Grid>
... other controls ...
<Image Name="img" Grid.Row="..." Grid.Column="..."
Width="100" Height="100" Margin="100,100,0,0"/>
</Grid>
Set Source in code:
var uri = new Uri("pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
img.Source = new BitmapImage(uri);
by default the window content is a grid so try
(this.Content as Grid).Children.Add(img);

How set dynamically windows back ground image in wpf

I have small doubt in wpf,i kept image on button.so when we click on button that image should be set as back ground?what should i do? any one have idea>
please let me know.
Thanks in Adavance
Developer
If you need to put an image as a background of any control (button or grid, didn't understancd you), just set, in code, the Background property to an ImageBrush with an ImageSource of your image.
This code is for button, edit it ...
Button button = new Button();
button.Margin = new Thickness(220, -880, 0, 0);
button.Width = w;
button.Height = h;
/////////////////////////////////////////////////////////
ImageBrush berriesBrush = new ImageBrush();
berriesBrush.ImageSource = new BitmapImage(new Uri(#"Image/country.PNG", UriKind.Relative));
button.Background = berriesBrush;
button.BorderThickness = new Thickness(0);
This might you ...

Creating Images as buttons in code in C# WPF

I want to create an image as a button in code in C# WPF (not a button with BG image but an actual image). I read on this site to use a PictureBox for the image, and I've found that the WPF equivalent is Image. The problem is, that while i've found PictureBox has a .Click that you can set, Image does not. The two things I want to do are:
Create an array of buttons that are images and can be clicked.
Have an image for the unclicked and clicked states of the button.
Is there anything right in front of me I'm missing?
Here is my loop creating the buttons:
sideBarButtons = new Button[infoLoader.categoriesLength];
sideButtons = new Image[infoLoader.categoriesLength];
ImageBrush[] myBg = new ImageBrush[infoLoader.categoriesLength];
for (int i = 0; i < sideBarButtons.Length; i++)
{
myBg[i] = new ImageBrush();
myBg[i].ImageSource = new BitmapImage(graphicLoader.buttonUnselected[(i % myBg.Length)]);
/*sideBarButtons[i] = new Button();
sideBarButtons[i].Content = infoLoader.categories[i].name;
sideBarButtons[i].Background = myBg[i];
//sideBarButtons[i].BorderThickness = ;
sideBarButtons[i].Width = 155;
sideBarButtons[i].Height = 46;
Canvas.SetLeft(sideBarButtons[i], 30);
Canvas.SetTop(sideBarButtons[i], 10 + (46 * i));
sideBarButtons[i].Click += new RoutedEventHandler(this.SideButton_Click);
leftSideBar.Children.Add(sideBarButtons[i]);*/
BitmapImage myBmp = new BitmapImage();
myBmp.BeginInit();
myBmp.UriSource = myBg[i];
myBmp.EndInit();
sideButtons[i] = new Image();
sideButtons[i].Source = myBmp;
sideButtons[i].Width = 155;
sideButtons[i].Height = 46;
Canvas.SetLeft(sideButtons[i], 30);
Canvas.SetTop(sideButtons[i], 10 + (46 * i));
sideButtons[i].Click += new RoutedEventHandler(this.SideButton_Click);
leftSideBar.Children.Add(sideButtons[i]);
}
The first commented out area is when I was creating buttons with buttons and not images, while the second is images and it doesn't work. Thanks in advance.
Two options here:
1.) Instead of using the Click event, which doesn't exist on Image, use MouseDown, which does.
2.) Instead of using Images and repurposing them, use Buttons with a custom style on it. Then you can handle the button's click.
Personally, I'd use the latter, but really either works.

Categories

Resources