样式标签宿主图像和文本

发布于 2024-12-02 11:15:19 字数 2464 浏览 0 评论 0原文

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

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

标签样式:

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

TextBlockStyle:

<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>

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

<Label Style="{StaticResource LabelStyle}">Create</Label>

文本创建确实不显示,但是如果我放入 LabelStyle->TextBlock->text 它会显示,但这不好,因为我想更改它以适应不同的标签。有没有办法将我的标签文本绑定到我的(内部样式)TextBlock.Text???

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

谢谢:-)

编辑:

这就是我现在实现的 HB 答案

    <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>

请注意,这是在资源字典中。对于 TextBlock 来说效果很好。但对于图像来说,情况就不同了。我想要与 'Text="{TemplateBinding Content}' 相同,但对于 Image.Source 并在添加标签时在我的控件中设置它的路径。可能因为它是多个内容,所以我必须编写比我更多的代码”我喜欢,但我会选择最简单、最干净的答案,

再次感谢HB,至于超链接,这仍在开发中,无论如何它都不会是超链接,只是一些带有动画的自定义菜单按钮。对于用户来说,这并不那么无聊:P

this is a two part question that probbably have a similar answer.

I want to create in a resource dictionary a style for a label that contains first an image and then the text. The text, as a TextBlock has it's own style (had no problems there). Here is what I have

Label Style:

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

TextBlockStyle:

<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>

Now my problem is when I add a new label to my Control (ex: Window) and specify the text (ex: Create), no text is shown.Something like:

<Label Style="{StaticResource LabelStyle}">Create</Label>

The text Create does not show, however if I put in my LabelStyle->TextBlock->text it shows, but it's no good since I want to change it for different labels. Is there a way to bind my Label text to my (Inside Style) TextBlock.Text???

My other question is the same, but for images, and Image.Source.

Thanks :-)

EDIT:

This is what I have now with H.B. answer implemented

    <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>

Note that this is in the Resource Dictionary. For the TextBlock it works great. But for the image it's a different story. I want the same as the 'Text="{TemplateBinding Content}' but for the Image.Source and set it's path in my control when I add the label. Probabbly since it's multiple content I'll have to write more code than I'd like, but I'll settle for the easiest, cleanest answer.

H.B. thanks again and as for the hyperlink, this is still in development, it's not going to be in anyway a hyperlink, just some custom menu button with some animation so it's not so boring for the user :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2024-12-09 11:15:19

您的 Label.Template 不再链接 LabelContent 属性(您将其设置为“Create”)到任何内部零件。要解决此问题,您可以像这样绑定 TextBlock.Text

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

(我刚刚注意到您使标签看起来像超链接,您确实意识到 已经有一个类,对吧?)

Your Label.Template no longer links the Content property of the Label (which you set to "Create") to any internal part. To fix this you can for example bind the TextBlock.Text like this:

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

(I just noticed that you make the Label look like a hyperlink, you do realize that there already is a class for that, right?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文