You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF自定义TabButton控件图片在父控件及运行时不显示求助

问题:WPF自定义TabButton控件图片显示异常

我在WPF程序中实现了标签页及用于切换标签的TabBar用户控件,TabBar中的按钮为自定义的TabButton用户控件。该控件包含背景图片、用于标识选中状态的半透明黑色图片、文本块以及顶层的透明点击按钮。

当前存在两个问题:

  • 背景图片在TabButton控件的设计器中可正常显示,但在TabBar和主窗口的设计器中无法显示(文本块和按钮显示正常);
  • 半透明图片默认设为隐藏,即使修改可见性,在TabButton设计器外及运行时也无法显示。

我已将图片的生成操作设置为Resource,清理并重建了解决方案,也检查了所有代码,但问题依旧。


相关代码

TabButton.xaml

<UserControl x:Class="EmeraldBattleFactoryHelper.Custom_Controls.TabButton"
             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:local="clr-namespace:EmeraldBattleFactoryHelper.Custom_Controls"
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="295">
    <Grid>
        <Image Source="/Custom Controls/Resources/tab.png" Stretch="Fill" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
        <Image x:Name="SelectionIndicator" Source="/Custom Controls/Resources/35percentblack.png" Stretch="Fill" Panel.ZIndex="1" Visibility="Hidden"/>
        <TextBlock Text="TEXT BLOCK" Background="Transparent" Panel.ZIndex="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Power Green"/>
        <Button Background="Transparent" BorderThickness="0" Panel.ZIndex="3" Click="Button_Click"/>
    </Grid>
</UserControl>

TabButton.xaml.cs

public partial class TabButton : UserControl
{
    public TabButton()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SelectionIndicator.Visibility = Visibility.Visible;
    }

    public void Deselect() 
    {
        SelectionIndicator.Visibility = Visibility.Hidden;
    }
}

TabBar.xaml

<UserControl x:Class="EmeraldBattleFactoryHelper.Custom_Controls.TabBar"
             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:local="clr-namespace:EmeraldBattleFactoryHelper.Custom_Controls"
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="1280">
    <Grid x:Name="TabBarGrid" SizeChanged="TabBarGrid_SizeChanged">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition x:Name="SettingsColumn"/>
        </Grid.ColumnDefinitions>
        <local:TabButton/>
        <local:TabButton Grid.Column="1"/>
        <local:TabButton Grid.Column="2"/>
        <local:TabButton Grid.Column="3"/>
        <local:TabButton Grid.Column="4"/>
    </Grid>
</UserControl>

TabBar.xaml.cs

public partial class TabBar : UserControl
{
    public TabBar()
    {
        InitializeComponent();
    }

    private void TabBarGrid_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        SettingsColumn.Width = new GridLength(TabBarGrid.ActualHeight);
    }

    private void ResetButtons() 
    {
        foreach (TabButton tabButton in TabBarGrid.Children) 
        {
            tabButton.Deselect();
        }            
    }
}

MainWindow.xaml

<Window x:Class="EmeraldBattleFactoryHelper.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:EmeraldBattleFactoryHelper"
        xmlns:customControls="clr-namespace:EmeraldBattleFactoryHelper.Custom_Controls"
        mc:Ignorable="d"
        Title="Battle Factory Helper" Height="720" Width="1280" ResizeMode="CanMinimize">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition x:Name="TabBar" MaxHeight="100" MinHeight="40" Height="15*"/>
            <RowDefinition Height="85*"/>
        </Grid.RowDefinitions>
        <customControls:TabBar/>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

设计器及运行时效果

  • TabButton设计器预览:
    TabButton设计器预览
  • TabBar设计器预览(添加背景以便查看文本):
    添加背景的TabBar设计器预览
  • TabBar设计器预览(无背景):
    无背景的TabBar设计器预览
  • 主窗口设计器预览:
    主窗口设计器预览
  • 运行时效果:
    运行时效果

内容的提问来源于stack exchange,提问作者Tilapia1124

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 09:07:05