简体   繁体   中英

Can't access the Text property of a TextBlock in a ControlTemplate

I'm trying to data bind a property to a Label and change its color and text in response to the value of the bound property. I'm using a ControlTemplate to change the color and Text because changing the Content of a Label in response to DataTriggers didn't work (the text never appeared).

So, using a ControlTemplate works when defining it inline on the Label, but does not seem to work when defining the template as a resource.

The code below is a simpler example to demonstrate the problem.

This is what I have so far:

    <ResourceDictionary>
        <ControlTemplate x:Key="baseTemplate" TargetType="{x:Type Label}">
            <Grid Background="{TemplateBinding Background}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="24"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="24"/>
                </Grid.ColumnDefinitions>
                <TextBlock x:Name="InnerTextBlock" Grid.Column="1"
                           Text="{TemplateBinding Label.Content}" <!-- An attempt to tie the Text here to the Label's Content property -->
                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                           Padding="{TemplateBinding Padding}"
                           Background="{TemplateBinding Background}"
                           Foreground="{TemplateBinding Foreground}"
                           Width="{TemplateBinding Width}"
                           Height="{TemplateBinding Height}"
                     />
            </Grid>
        </ControlTemplate>

        <Style x:Key="availableLabelStyle" TargetType="{x:Type Label}">
            <Setter Property="Background" Value="#FF567E4A"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Content" Value="Available"/>
            <Setter Property="Template" Value="{StaticResource baseTemplate}"/>
        </Style>
    </ResourceDictionary>

    <Label x:Name="StatusLabel"
           Style="{StaticResource availableLabelStyle}"
           Grid.Column="1"
           HorizontalAlignment="Left"
           Margin="111,71,0,0"
           VerticalAlignment="Top" Width="124"
           HorizontalContentAlignment="Center"
           VerticalContentAlignment="Center"
           Height="18"
           Padding="2"
    />

The problem is that the Content property in the Setter for the 'availableLabelStyle' does not seem to work. No text appears when this style is applied to a Label.

Is there a better way to do the same thing here AND get the text to appear in the Label?

Thanks in advance for any help on this.

The code you have is working. Here is my complete example:

<Window x:Class="WPFTestApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
    <ControlTemplate x:Key="baseTemplate" TargetType="{x:Type Label}">
        <Grid Background="{TemplateBinding Background}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="24"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="24"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="1" Text="{TemplateBinding Label.Content}"
                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                           Padding="{TemplateBinding Padding}"
                           Background="{TemplateBinding Background}"
                           Foreground="{TemplateBinding Foreground}"
                           Width="{TemplateBinding Width}"
                           Height="{TemplateBinding Height}"/>
        </Grid>
    </ControlTemplate>

    <Style x:Key="availableLabelStyle" TargetType="{x:Type Label}">
        <Setter Property="Background" Value="#FF567E4A"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Content" Value="Available"/>
        <Setter Property="Template" Value="{StaticResource baseTemplate}"/>
    </Style>
</ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Label x:Name="StatusLabel" Style="{StaticResource availableLabelStyle}"
           Grid.Column="1"
           HorizontalAlignment="Left"
           Margin="111,71,0,0"
           VerticalAlignment="Top" Width="124"
           HorizontalContentAlignment="Center"
           VerticalContentAlignment="Center"
           Height="18"
           Padding="2"/>
    </Grid>
</Window>

Which produces the following output:

在此输入图像描述

Another way to do this is to use ContentPresenter instead of TextBlock . You can still add all of your additional properties to it (at least the ones you have shown), and it would allow displaying content other than text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM