在WPF中未正确对齐的按钮

时间:2014-05-28 06:48:05

标签: c# wpf canvas

自定义控件有两个图像,一个是背景图像和一个前景图像。我使用以下代码来显示控件。此功能被调用两次,一次用于背景图像显示,然后用于前景图像显示

                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

按钮显示完美,但我意识到它们的对齐方式不正确。当我在另一个按钮的正下方放置一个按钮时,下面的一个按钮似乎向右移动。

通过更改

来纠正这种对齐问题
                Image image = new Image();
                image.Source = ib.ImageSource;
                gr.Children.Add(image);

                gr.Background = ib;

但是,这会使前景图像完全覆盖背景图像,就像我问过here

一样

我该怎么做才能防止这种对齐问题?还有,首先是什么导致了这个问题?

1 个答案:

答案 0 :(得分:1)