自定义控件有两个图像,一个是背景图像和一个前景图像。我使用以下代码来显示控件。此功能被调用两次,一次用于背景图像显示,然后用于前景图像显示
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
一样我该怎么做才能防止这种对齐问题?还有,首先是什么导致了这个问题?
答案 0 :(得分:1)
您是否尝试将按钮放在stackpanel内? http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.aspx