Wpf 样式标签宿主图像和文本

Wpf 样式标签宿主图像和文本,wpf,xaml,styles,label,resourcedictionary,Wpf,Xaml,Styles,Label,Resourcedictionary,这是一个由两部分组成的问题,可能有类似的答案 我想在资源字典中创建一个标签样式,该标签首先包含图像,然后包含文本。文本,作为一个文本块,有它自己的风格(没有问题)。这是我的 标签样式: <Style x:Key="LabelStyle" TargetType="Label"> <Setter Property="Template"> <Setter.Value> <ControlTemplate Targe

这是一个由两部分组成的问题,可能有类似的答案

我想在资源字典中创建一个标签样式,该标签首先包含图像,然后包含文本。文本,作为一个文本块,有它自己的风格(没有问题)。这是我的

标签样式:

<Style x:Key="LabelStyle" TargetType="Label">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Label">
                <TextBlock Style="{StaticResource TextBlockStyle}">
                </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

文本块样式:

<Style x:Key="TextBlockStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="Margin" Value="25 0 0 2.5"/>
    <Setter Property="Width" Value="Auto"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="TextDecorations" Value="Underline"/>
            <Setter Property="Foreground" Value="Blue"/>
            <Setter Property="Cursor" Value="Hand"/>
        </Trigger>
    </Style.Triggers>
</Style>

现在我的问题是,当我向控件(ex:Window)添加新标签并指定文本(ex:Create)时,不会显示任何文本。类似于:

<Label Style="{StaticResource LabelStyle}">Create</Label>
创建
文本Create不会显示,但是如果我在LabelStyle->TextBlock->text中输入它显示的文本,但是这没有用,因为我想为不同的标签更改它。有没有办法将我的标签文本绑定到我的(内部样式)文本块。文本

我的另一个问题是相同的,但对于图像和Image.Source

谢谢:-)

编辑:

这就是我现在实施的H.B.答案

    <Style x:Key="LabelStyle" TargetType="Label">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Label">
                <Grid>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/Resources/Create.png" />
                        <TextBlock Style="{StaticResource TextBlockStyle}" Text="{TemplateBinding Content}"/>
                    </StackPanel>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

请注意,这在资源字典中。对于文本块,它非常有效。但对于图像来说,这是一个不同的故事。我希望与'Text=“{TemplateBinding Content}”相同,但是对于图像。当我添加标签时,在我的控件中源并设置它的路径。可能因为它是多个内容,我必须编写比我想要的更多的代码,但我会满足于最简单、最干净的答案


H.B.再次感谢,至于超链接,它仍在开发中,它不会是一个超链接,只是一些带有动画的自定义菜单按钮,这样用户就不会觉得无聊了:p

你的
标签。模板
不再链接
标签
内容
属性(你设置为
)在任何内部零件上创建“
”。要解决此问题,您可以像这样绑定
TextBlock.Text

<ControlTemplate TargetType="Label">
    <TextBlock Style="{StaticResource TextBlockStyle}"
               Text="{TemplateBinding Content}"/>
</ControlTemplate>


(我刚注意到你把标签做成了超链接,你意识到了吗?

谢谢,这正是我需要的:))至于图像?是相同的(或相似的)??你对图像做了什么,你改变了它的模板吗?请提供尽可能多的信息。@loxxy:我想它失败了(听了这话,显然很沉默)。如果你有这样的内容,你可以创建一个,在那里你可以为
TextBlock.Text
Image.Source
创建一个属性。你也可以滥用Image.Source-TemplateBinding的属性。另一个替代方法是使用动态资源,如图所示。谢谢,我想使用UserControl。它更简单,快捷r、 更多的可定制和更少的代码行。非常感谢