开发者

Style label host image and text

开发者 https://www.devze.com 2023-04-01 21:07 出处:网络
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 TextBloc

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"/>
   开发者_如何学Go <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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号