代码方式给控件添加背景图片(WPF)

      wpf中常常需要给控件添加背景图片,下边以wrapPanel为例,使用代码添加背景图片

         WrapPanel xwraPanel = new WrapPanel();

            ImageBrush ximgBrush = new ImageBrush();

           Uri xuri = new Uri("pack://application:,,,/Images/JXimgs/MeterLayout.png", UriKind.Absolute); //注意这个的写法      

     ximgBrush.ImageSource = new BitmapImage(xuri);        

     xwraPanel.Background = ximgBrush;

另外我们在项目中可以设置控件的BackGround=“Transparent” 使本控件透明,而显示父容器的背景色

原文地址:https://www.cnblogs.com/hanjiaxu/p/4432335.html