带图像的wpf选项卡项

带图像的wpf选项卡项,wpf,styles,wpf-controls,tabitem,Wpf,Styles,Wpf Controls,Tabitem,我需要使用带有mahapps.metro的stile,带有文本和图像的tabitem。。 这是我的代码: <TabItem Style="{StaticResource gMetroTabItem}"> <TabItem.Header> <StackPanel Orientation="Horizontal"> <Image Name="img" Height="auto" Width="auto" Sou

我需要使用带有mahapps.metro的stile,带有文本和图像的tabitem。。 这是我的代码:

<TabItem Style="{StaticResource gMetroTabItem}">
    <TabItem.Header>
        <StackPanel Orientation="Horizontal">
            <Image Name="img" Height="auto" Width="auto" Source="/myProject;component/Resources/Black_Tools.png" />
            <TextBlock Text="tabItem2" Margin="2,0,0,0" VerticalAlignment="Center" />
        </StackPanel>
    </TabItem.Header>
</TabItem>

这是样式的代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type TabControl}" x:Key="gMetroTabControl">
        <Setter Property="Background" Value="{x:Null}" />
        <Setter Property="BorderBrush" Value="{x:Null}" />
    </Style>

    <Style TargetType="TabItem" x:Key="gMetroTabItem">
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Padding" Value="6,2,6,2" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
        <Setter Property="MinWidth" Value="5" />
        <Setter Property="MinHeight" Value="5" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TabItem">
                    <Label x:Name="root" FontSize="46.67">
                        <ContentPresenter ContentSource="Header"  RecognizesAccessKey="True" />
                    </Label>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="root" Property="Foreground">
                                <Setter.Value>
                                    <SolidColorBrush Color="{DynamicResource AccentColor}" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>

                        <Trigger Property="IsSelected" Value="false">
                            <Setter  TargetName="root" Property="Foreground">
                                <Setter.Value>
                                    <SolidColorBrush Color="{DynamicResource GrayNormal}" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger SourceName="root" Property="IsMouseOver" Value="True">
                            <Setter  TargetName="root" Property="Foreground">
                                <Setter.Value>
                                    <SolidColorBrush Color="Lime" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <!--<Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <AdornerDecorator>
                        <ContentPresenter Content="{Binding}"/>
                    </AdornerDecorator>
                </DataTemplate>
            </Setter.Value>
        </Setter>-->
    </Style>
</ResourceDictionary>

但不起作用,因为样式会更改文本属性,而图像不会显示


有什么想法吗?

把我之前说的话记下来。这要容易得多

您只需要TabControl.ItemTemplate。这是决定页眉布局的因素。通过一些复杂的祖先绑定,您可以绑定到DataTriggers的TabItem属性

在这个例子中,我展示了如何绑定到IsSelectedProperty。我将把这项运动留给你做练习。提示:使用ElementName“root”绑定到IsMouseOver,然后在setter中使用TargetName=“root”

然后您只需像这样设置dataobject中的值

<TabControl Style="{StaticResource gMetroTabControl}">
    <local:TabItemHeaderData RectColor="Black" LabelText="John">
        <local:TabItemHeaderData.Content>
            <Button>George</Button>
        </local:TabItemHeaderData.Content>
    </local:TabItemHeaderData>
    <local:TabItemHeaderData RectColor="Black" LabelText="John">
    </local:TabItemHeaderData>
</TabControl>

乔治

来自您发布的屏幕广播图像:

  • 设计时间:
  • 运行时:
这表明
Black_Tools.png
的属性不正确。确保将映像设置为资源并复制到输出目录:

  • 右键单击解决方案资源管理器>属性中的
    Black\u Tools.png
  • 构建操作:资源
  • 复制到输出目录:始终复制(或复制,如果更新)

如果该映像未设置为资源并复制到输出目录,那么您将在设计时看到该映像,因为该映像可以在解决方案中解析。但是,在运行时,映像路径不同,因为文件将位于项目的输出目录中。

请发布代码好吗?鼠标悬停并单击文本的颜色即可使用?确定。你需要把确切的问题放在问题中。你没有。你说图像没有显示。我假设头中的图像来自XAML的第一个块。该图像显示良好。这是我看到的设计时间:这是我看到的运行时。。。如果您将标题保留为文本。它将正确地填充标签。该标签将根据您的需要高亮显示。但它不在账单里。您必须使用headerTemplate“Copy to Output Directory”值如何?我刚刚使用了您的精确代码,MahApps样式运行良好。我还能够在运行时通过清理解决方案并将映像生成操作设置为“无”,运行应用程序(无映像),然后将生成操作设置为“资源”,运行应用程序(仍无映像),来重现丢失的映像。解决方案是重新清理解决方案,将构建操作设置为资源,再次清理,然后运行。一切都很顺利。
public class TabItemHeaderData
{
    public Brush RectColor { get; set; }
    public String LabelText { get; set; }
    public object Content { get; set; }
}
<TabControl Style="{StaticResource gMetroTabControl}">
    <local:TabItemHeaderData RectColor="Black" LabelText="John">
        <local:TabItemHeaderData.Content>
            <Button>George</Button>
        </local:TabItemHeaderData.Content>
    </local:TabItemHeaderData>
    <local:TabItemHeaderData RectColor="Black" LabelText="John">
    </local:TabItemHeaderData>
</TabControl>