asim mittal

asim mittal

  • NA
  • 3
  • 21.7k

C# as opposed to XAML

Oct 21 2010 5:27 PM
Hi,

I've been playing around with WPF lately, and its nice to use XAML. But XAML is helpful only when the layout is not yet done. It is, after all a markup language. What i want to achieve is loading an image when the window loads, creating a white border around that image, and showing the two on the main window.

I followed the tree structure that XAML uses, but somehow everytime i debug, nothing is visible on the form... why is this happening?

namespace Images_and_Borders
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Grid grid = new Grid();
Image image = new Image();
BitmapImage bitmap = new BitmapImage(new Uri("d:\\image.jpg"));
Border imageBorder = new Border();
double aspectRatio = 0;

image.Source = bitmap;
aspectRatio = image.Width / image.Height;
imageBorder.Height = 200;
imageBorder.Width = imageBorder.Height * aspectRatio;

imageBorder.Child = image;
grid.Children.Add(imageBorder);
}


}
}

As you can see i create a Grid, then an image, then a border and basically recreate the tree as it would have been done in XML  image ==child of ==> border == child of ==> grid

But when i run this, i get a blank window. I don't want to forcibly put controls on my window. Later on I'd like to make my own custom image class which would essentially contain a border and an image.  Any clues about why i'm not able to see it?

I'm obviously doing something wrong.

Thanks,
Mittal

Answers (2)