样式标签主机图像和文本

时间:2011-09-02 17:22:24

标签: wpf xaml styles label resourcedictionary

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

我想在资源字典中创建一个标签的样式,该标签首先包含图像,然后是文本。文本,作为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>

现在我的问题是当我向我的Control添加一个新标签(例如:Window)并指定文本(例如:Create)时,不会显示任何文字。例如:

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

文本创建没有显示,但是如果我放入我所显示的LabelStyle-&gt; TextBlock-&gt;文本,但它不好,因为我想为不同的标签更改它。有没有办法将我的Label文本绑定到我的(Inside Style)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>

请注意,这是在资源字典中。对于TextBlock,它很棒。但对于图像来说,这是一个不同的故事。我想要与'Text =“{TemplateBinding Content}'相同,但是对于Image.Source并在我添加标签时设置它在我的控制中的路径。可能因为它是多个内容我将不得不写更多代码而不是我'我愿意,但我会满足于最简单,最干净的答案。

H.B。再次感谢,至于超链接,这仍然在开发中,它不会是一个超级链接,只是一些带有动画的自定义菜单按钮,所以对用户来说不是那么无聊:P

1 个答案:

答案 0 :(得分:1)

您的Label.Template不再将Content(您设置为Label)的"Create"媒体资源与任何内部部分相关联。要解决此问题,您可以绑定TextBlock.Text,例如:

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

(我只是注意到你让Label看起来像一个超链接,你确实认识到there already is a class,对吗?)

相关问题