C# 带关闭按钮的WPF自定义选项卡项
我正在使用WPF和C。我正在尝试创建一个包含关闭按钮的自定义选项卡项。我确实遵循了一个教程,并设法让它工作-但我想使用我的关闭按钮的图像。在尝试这一点时,我遇到了一些例外 System.Deployment.dll中发生类型为“System.Deployment.Application.InvalidDeploymentException”的首次意外异常 mscorlib.dll中发生类型为“System.IO.DirectoryNotFoundException”的第一次意外异常 PresentationCore.dll中首次出现类型为“System.IO.DirectoryNotFoundException”的异常 mscorlib.dll中发生类型为“System.IO.DirectoryNotFoundException”的第一次意外异常 我相信这是在抱怨没有找到图像源。为了设置图像源,我使用属性窗格并在源下拉菜单中选择适当的资源。这应该是很好的,但一点也不好 我想知道是否有人能告诉我该怎么做?这很奇怪,因为如果它说找不到资源,那么资源就在那里。我使用Resources.resx添加了这些图像,它将它们添加到了我的Resources文件夹中。一切都在那里。。。只是我的XAML不喜欢它 这是我的XAMLC# 带关闭按钮的WPF自定义选项卡项,c#,wpf,xaml,tabitem,C#,Wpf,Xaml,Tabitem,我正在使用WPF和C。我正在尝试创建一个包含关闭按钮的自定义选项卡项。我确实遵循了一个教程,并设法让它工作-但我想使用我的关闭按钮的图像。在尝试这一点时,我遇到了一些例外 System.Deployment.dll中发生类型为“System.Deployment.Application.InvalidDeploymentException”的首次意外异常 mscorlib.dll中发生类型为“System.IO.DirectoryNotFoundException”的第一次意外异常 Presen
<UserControl x:Class="WpfApplication1.CloseableHeader"
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"
mc:Ignorable="d"
d:DesignHeight="23" d:DesignWidth="81" Margin="0">
<UserControl.Resources>
<Style TargetType="Button" x:Key="close_hover">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Image Source="pack://siteoforigin:,,,/Resources/CloseIco.png" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="Button" x:Key="close_normal">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Image Source="pack://siteoforigin:,,,/Resources/CloseIco_BW.png" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<Button
Name="button_close"
ToolTip="Close"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Margin="0,0,5,0"
Width="14"
Height="14"
Style="{DynamicResource close_normal}"/>
<Label Content="TabItem" Height="23" HorizontalAlignment="Left"
Margin="4,1,0,0" Name="label_TabTitle" VerticalAlignment="Top"
FontFamily="Courier" FontSize="12" />
</Grid>
我还应该注意的是,在VisualStudio的Designer视图中,它按应有的方式显示选项卡(包括图像!)
提前谢谢
更新
我现在在我的项目中走得更远了,我面临的唯一问题是Visual Studio上的路径不存在,但当我调试时,它们确实存在。资源文件夹位于bin/Debug中,因此图像将在调试时显示。。。但这意味着我在设计器中总是会有错误。有没有办法解决这一问题,使之对双方都有效 这是更新的XAML(相当大的变化)
使用
Stackpanel
组件,可以更轻松地设置按钮的图像(或任何您想要的内容)。在这里,按钮将“close.ico”作为图标,“close”作为水平排列的文本
回到你的问题,通过包URI设置资源地址,你不会看到它的显示(实际上是加载的),除非你对嵌入的资源/内容构建操作耍一些把戏,但是你需要为应用程序提供文件;
当您运行应用程序时,请改用直接链接(Resources\close.ico)与在设计器中看到应用程序时不同。因此,您必须从应用程序的输出目录中看到,您的想象是可访问的。假设您的输出在某个地方是bin/Debug。您的映像位于/Resources/CloseIco_BW.png中,您可以从输出目录访问它吗?请尽量不要在WPF中使用EmbeddedResource的BuildAction。据我所知,应用程序所需的任何资源都应该标记为“Compile”或“Resource”,它与resources.resx文件无关。所以,只需将新的资源文件夹添加到您的项目中,并将所有图像复制到该文件夹中即可。接下来,在按钮模板中将图像的源设置为“Resources\yourmagename.png”,就完成了。您可能还需要使用适当的相对路径来查找资源文件夹,这样,如果您的用户控件位于项目层次结构的某个深处,则源可能会变成“..\..\Resources\yourimagename.png”。看起来不错,我将resources文件夹移动到bin\Debug中,调试似乎很好,尽管Visual Studio将永久性地抱怨无法找到图像。有办法解决这个问题吗?那么Resources.resx的实际用途是什么?此外,选项卡的样式也不是它应该的样式。请看这张图片。左边是标签应该是什么样子(但是有了关闭的图像),右边是它实际出现的样子。有什么想法吗?谢谢。您已将图像移动到bin/Debug中,但有一种可能更好的方法。将所有这些图像添加到VisualStudio中的实际项目中,并将它们的BuildAction设置为“资源”(但不是EmbeddedResource)。它们将被编译到您的exe文件中,并且始终存在,设计者应该正确地显示它们。我发现这很有用-
<UserControl x:Class="WpfApplication1.CloseableHeader"
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"
mc:Ignorable="d"
d:DesignHeight="23" Margin="0">
<UserControl.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="border"
BorderThickness="0"
>
<Border.Background>
<ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/CloseIco_BW.png" />
</Border.Background>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="border">
<Setter.Value>
<ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/CloseIco.png" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid Background="White">
<Button
Name="button_close"
ToolTip="Close"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Margin="0,0,5,0"
Width="14"
Height="14"/>
<Label Content="TabItem" Height="23" HorizontalAlignment="Left"
Margin="4,1,19,0" Name="label_TabTitle" VerticalAlignment="Top"
FontFamily="Courier" FontSize="12" />
</Grid>
<Button Name="button_close">
<StackPanel Orientation="Horizontal">
<Image Source="Resources\close.ico" Width="14" Height="14" />
<TextBlock>
<Run Text="Close" />
</TextBlock>
</StackPanel>
</Button>