Paul
Paul

Reputation: 121

Buttons not aligned correctly in WPF

The customized controls have two images, a background and a foreground image. I use the following code to display the controls. This function is called twice, once for background image displaying and then for the foreground image displaying

                BitmapImage src = new BitmapImage();
                src.BeginInit();
                src.UriSource = new Uri(imgpath, UriKind.Absolute);
                src.CacheOption = BitmapCacheOption.OnLoad;
                src.EndInit();
                ImageBrush ib = new ImageBrush(src);
                ib.Viewbox = new Rect(UVRectangle.X / src.PixelWidth, UVRectangle.Y / src.PixelHeight, UVRectangle.Width / src.PixelWidth, UVRectangle.Height / src.PixelHeight);
                Image image = new Image();
                image.Source = ib.ImageSource;
                gr.Children.Add(image); //gr is of type Grid

The buttons are displayed perfectly but I realized that their alignment is incorrect. When I place one button right below another button, the one below appears to be shifted to the right.

This problem of alignment is rectified by changing

                Image image = new Image();
                image.Source = ib.ImageSource;
                gr.Children.Add(image);

to

                gr.Background = ib;

But, this makes the foreground image completely cover the background image like I had asked here

What should I do to prevent this alignment problem? Also, what is causing this problem in the first place?

Upvotes: 0

Views: 196

Answers (1)

pandeSai
pandeSai

Reputation: 117

Did you try putting the buttons inside stackpanel? http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.aspx

Upvotes: 1

Related Questions