如何在 Visual Studio 扩展中使用图像替换默认按钮样式?

huangapple go评论55阅读模式
英文:

How to replace default button style in visual studio extension with image?

问题

我尝试在Visual Studio扩展中删除默认的按钮样式,并用图像替换它。确切地说,我试图让我的按钮看起来像图像,而不像通常做所有其他扩展的按钮等等。

在预览中,我可以看到正确的图像,并且所有样式都被移除。但是当我开始调试时,图像就消失了。

有没有办法修复它?

<UserControl x:Class="TabsGroupper.TabsGroupperToolControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:imaging="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.Imaging"
             xmlns:theming="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Imaging"
             xmlns:util="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Utilities"
             xmlns:catalog="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.ImageCatalog"
             xmlns:toolkit="clr-namespace:Community.VisualStudio.Toolkit;assembly=Community.VisualStudio.Toolkit"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300"
             Name="TabGroupperWindow" 
			 FontFamily="Segoe UI">
	<Grid Background="#e8ecf4">
		<Grid.Resources>
			<Style x:Key="IconButtonStyle" TargetType="Button">
				<Setter Property="Template">
					<Setter.Value>
						<ControlTemplate TargetType="Button">
							<Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
								<Image Source="pack://application:,,,/TabsGroupper;component/Resources/Delete.png" Width="16" Height="16" />
							</Border>
						</ControlTemplate>
					</Setter.Value>
				</Setter>
			</Style>
		</Grid.Resources>
		<Grid.ColumnDefinitions>
			<ColumnDefinition></ColumnDefinition>
		</Grid.ColumnDefinitions>
		<StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="3">
			<Button Name="RemoveItemBtn" Style="{StaticResource IconButtonStyle}"></Button>
		</StackPanel>
	</Grid>
</UserControl>

尝试但没有结果的方法:

  • 将图像设置为内容
  • 使用ImageBrush设置背景
  • 为图像设置绝对路径
  • 将图像的生成操作设置为'resource'。
  • 重新启动Visual Studio
  • 重置Visual Studio实验实例
英文:

I try to remove default button style in visual studio extension and replace it with image. Exactly I try to make my button to look like image, not like button as usually do all other extensions and so on.

In preview I can see correct image and all styles are removed. But when I start debugging image just disappears.

Is there any way to fix it?

<UserControl x:Class="TabsGroupper.TabsGroupperToolControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:imaging="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.Imaging"
             xmlns:theming="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Imaging"
             xmlns:util="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Utilities"
             xmlns:catalog="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.ImageCatalog"
             xmlns:toolkit="clr-namespace:Community.VisualStudio.Toolkit;assembly=Community.VisualStudio.Toolkit"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300"
             Name="TabGroupperWindow" 
			 FontFamily="Segoe UI">
	<Grid Background="#e8ecf4">
		<Grid.Resources>
			<Style x:Key="IconButtonStyle" TargetType="Button">
				<Setter Property="Template">
					<Setter.Value>
						<ControlTemplate TargetType="Button">
							<Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
								<Image Source="pack://application:,,,/TabsGroupper;component/Resources/Delete.png" Width="16" Height="16" />
							</Border>
						</ControlTemplate>
					</Setter.Value>
				</Setter>
			</Style>
		</Grid.Resources>
		<Grid.ColumnDefinitions>
			<ColumnDefinition></ColumnDefinition>
		</Grid.ColumnDefinitions>
		<StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="3">
			<Button Name="RemoveItemBtn" Style="{StaticResource IconButtonStyle}"></Button>
		</StackPanel>
	</Grid>
</UserControl>

Tried with zero result:

  • set image as content
  • set background with imagebrush
  • set absolute path for image
  • set image build action to 'resource'.
  • restarted VS
  • reset of VS experimental instance

答案1

得分: 0

<Image Source="pack://application:,,,/TabsGroupper;component/Resources/Delete.png">

这个变种有效,问题出在图像上。要使其在图像的属性中工作,Build Action 必须设置为 Resource。另外,如果有多个图像,每个图像都应该将 Build Action 设置为 Resource

英文:
&lt;Image Source=&quot;pack://application:,,,/TabsGroupper;component/Resources/Delete.png&quot;&gt;

This variant works, the problem was about the image. To make it work in properties of image Build Action should be set to Resource. And in addition if there are several images than each of should have Build Action as Resource.

答案2

得分: -1

在ControlTemplate中,我尝试将图像源设置为:

Source="pack://application:,,,/Delete.png"

在项目目录中拥有PNG文件,并且对我有效...

英文:

In the ControlTemplate, I tried as Image Source

Source=&quot;pack://application:,,,/Delete.png&quot;

having the png file in the project directory and it worked for me...

huangapple
  • 本文由 发表于 2023年7月28日 04:53:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定