如何在代码隐藏中设置边框背景图像画笔
我有一个在 Windows 应用程序中使用的 WPF 自定义用户控件。该控件有一个边框作为主要元素,并且该边框有一个默认的背景图像。下面的代码显示了如何将此图像设置为默认图像。默认图像是资源元素 (Images/BlueRoad.jpg)。
我希望能够使用图像文件名作为字符串(例如“C:\Pictures\myCustomPic.bmp”)以编程方式更改边框背景的图像。我需要使用 Visual Basic 在代码隐藏中执行此操作,除非有非常简单的方法可以在 XAML 中执行此操作。无论哪种方式,图片都会加载到控件的启动代码中。
我对 WPF 不太了解,这只是应用程序的一个小元素,因此希望尽可能简单、快速地完成此任务。
非常感谢!
<Border Name="mainBorder" Opacity="1" BorderBrush="SteelBlue" BorderThickness="3">
<Border.Background>
<ImageBrush ImageSource="Images/BlueRoad.jpg"></ImageBrush>
</Border.Background>
Grid and other stuff goes here...
</Border>
I have a WPF custom user control that is used in a Windows Application. The control has a border as the main element, and this border has a default background image. The code below shows how this image is set as a default. The default image is a resource element (Images/BlueRoad.jpg).
I want to be able to programmatically change the image of the border background using an image filename as string (e.g. "C:\Pictures\myCustomPic.bmp"). I need to do this in code-behind using Visual Basic, unless there is a VERY simple way to do it in XAML. Either way, the picture will load in the startup code for the control.
I do not know much about WPF and this is just a small element of the application, so want to get this done as simply and quickly as possible.
Many Thanks!
<Border Name="mainBorder" Opacity="1" BorderBrush="SteelBlue" BorderThickness="3">
<Border.Background>
<ImageBrush ImageSource="Images/BlueRoad.jpg"></ImageBrush>
</Border.Background>
Grid and other stuff goes here...
</Border>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 ImageBrush 和 BitmapImage 将画笔设置为边框背景
首先,使用 uri 创建 BitmapImage 并将此 BitmapImage 发送到 ImageBrush 并将 ImageBrush 分配给边框背景
you can use ImageBrush and BitmapImage to set brush to border background
first you create BitmapImage with uri and send this BitmapImage to ImageBrush and assign ImageBrush to border background