为 MenuItem 设置图标 VB
Set icon for MenuItem VB
我需要从 WPF 中的隐藏代码创建上下文菜单。
除了图标外,一切都很好,我像这样设置 MenuItem 图标
Dim tsmi As New MenuItem() With {
.Header = cmd.Name,
.Icon = cmd.Icon,
.Tag = cmd
}
其中 cmd.Icon 是 System.Drawing.Image。
我得到的不是图标而是字符串 System.Drawing.Image,它应该是图像。
有人可以帮忙吗?
System.Drawing.Image
来自 WinForms,你需要的是 System.Windows.Controls.Image
.
你可以这样制作:
New Image() With {.Source = New BitmapImage(New Uri("pack://application:,,,/Your.Assembly.Name;component/Images/image.png"))}
...在程序集 Your.Assembly.Name.dll
.
的文件夹 Images
中有一个名为 image.png
的文件(标记为 Build Action=Resource)
MenuItem
文档显示了这个 XAML:
<MenuItem Header="New">
<MenuItem.Icon>
<Image Source="data/cat.png"/>
</MenuItem.Icon>
</MenuItem>
因此您可以清楚地为图标使用 WPF Image
控件。 Image.Source
属性 的文档为标题为 "How to: Use the Image Element" 的主题提供了 link,其中包括以下代码示例:
' Create Image Element
Dim myImage As New Image()
myImage.Width = 200
' Create source
Dim myBitmapImage As New BitmapImage()
' BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit()
myBitmapImage.UriSource = New Uri("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg")
' To save significant application memory, set the DecodePixelWidth or
' DecodePixelHeight of the BitmapImage value of the image source to the desired
' height or width of the rendered image. If you don't do this, the application will
' cache the image as though it were rendered as its normal size rather then just
' the size that is displayed.
' Note: In order to preserve aspect ratio, set DecodePixelWidth
' or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200
myBitmapImage.EndInit()
'set image source
myImage.Source = myBitmapImage
这几乎可以满足您的所有需求。我以前从未使用过任何这些类型或成员。我只是花了一些时间阅读相关文档。
我需要从 WPF 中的隐藏代码创建上下文菜单。 除了图标外,一切都很好,我像这样设置 MenuItem 图标
Dim tsmi As New MenuItem() With {
.Header = cmd.Name,
.Icon = cmd.Icon,
.Tag = cmd
}
其中 cmd.Icon 是 System.Drawing.Image。 我得到的不是图标而是字符串 System.Drawing.Image,它应该是图像。 有人可以帮忙吗?
System.Drawing.Image
来自 WinForms,你需要的是 System.Windows.Controls.Image
.
你可以这样制作:
New Image() With {.Source = New BitmapImage(New Uri("pack://application:,,,/Your.Assembly.Name;component/Images/image.png"))}
...在程序集 Your.Assembly.Name.dll
.
Images
中有一个名为 image.png
的文件(标记为 Build Action=Resource)
MenuItem
文档显示了这个 XAML:
<MenuItem Header="New">
<MenuItem.Icon>
<Image Source="data/cat.png"/>
</MenuItem.Icon>
</MenuItem>
因此您可以清楚地为图标使用 WPF Image
控件。 Image.Source
属性 的文档为标题为 "How to: Use the Image Element" 的主题提供了 link,其中包括以下代码示例:
' Create Image Element
Dim myImage As New Image()
myImage.Width = 200
' Create source
Dim myBitmapImage As New BitmapImage()
' BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit()
myBitmapImage.UriSource = New Uri("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg")
' To save significant application memory, set the DecodePixelWidth or
' DecodePixelHeight of the BitmapImage value of the image source to the desired
' height or width of the rendered image. If you don't do this, the application will
' cache the image as though it were rendered as its normal size rather then just
' the size that is displayed.
' Note: In order to preserve aspect ratio, set DecodePixelWidth
' or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200
myBitmapImage.EndInit()
'set image source
myImage.Source = myBitmapImage
这几乎可以满足您的所有需求。我以前从未使用过任何这些类型或成员。我只是花了一些时间阅读相关文档。