[英]WPF Custom TabItem with Close Button
我正在使用WPF和C#。 我试图创建一个包含关闭按钮的自定义TabItem。 我确实遵循了一个教程并设法使其正常工作-但是我想使用“图像”作为关闭按钮。 尝试此操作后,我遇到了一些例外情况。
System.Deployment.dll中发生类型'System.Deployment.Application.InvalidDeploymentException'的第一次机会异常mscorlib.dll中发生类型'System.IO.DirectoryNotFoundException'的第一次机会异常类型'System.IO PresentationCore.dll中发生“ .DirectoryNotFoundException”。mscorlib.dll中发生了“ System.IO.DirectoryNotFoundException”类型的首次机会异常
我相信这是在抱怨找不到图像源。 要设置图像源,我使用了属性窗格,并在“源”下拉菜单中选择了适当的资源。 这应该是正确的,但是一点也不好。
我想知道是否有人可以建议我做些什么? 这很奇怪,因为如果它说找不到资源,那么资源就在那里。 我使用Resources.resx添加了图像,并将其添加到我的Resources文件夹中。 一切都在那里...只是我的XAML不喜欢它。
这是我的XAML
<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>
我还应该注意,在Visual Studio的Designer视图中,它按其应有的方式显示选项卡(带有图像!)。
提前致谢。
更新
我现在在项目中走得更远,我面临的唯一问题是Visual Studio上的路径不存在,但是当我调试时-它们确实存在。 resources文件夹位于bin / Debug中,因此图像将在调试时显示...但这意味着我在Designer中始终会出现错误。 有没有一种方法可以解决此问题,使其同时适用于两者?
这是更新的XAML(相当大的变化)
<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>
使用Stackpanel
组件,可以使用一种更简单的方法将Image(或任意)设置为按钮。 在这里,按钮将以“ close.ico”为图标,以“ Close”为水平排列的文本
<Button Name="button_close"> <StackPanel Orientation="Horizontal"> <Image Source="Resources\\close.ico" Width="14" Height="14" /> <TextBlock> <Run Text="Close" /> </TextBlock> </StackPanel> </Button>
回到您的问题,通过pack-URI设置资源地址,您将不会显示它(实际上是已加载),除非您对嵌入式资源/内容构建操作起了一定作用-但随后您需要为该文件提供应用程序; 改用直接链接(Resources \\ close.ico)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.