简体   繁体   中英

WPF Label DataTemplate

I am trying to define a datatemplate for a label. Is this possible? Since I could not find anything helpful to move forward I defined a control template and tried to get things working. Unfortunately when I bind two items as follows I get the label displaying nothing, even though an object with the given parameters has been successfully bound to the label from the code behind. Please help!

<Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <Border>
                        <Grid>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                            </Grid.ColumnDefinitions>

                            <TextBlock Background="Green" Text="{Binding TableName}" Grid.Column="0"></TextBlock>
                            <TextBlock Background="Red" Text="{Binding ColumnName}"  Grid.Column="1"></TextBlock>

                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

Edited: Full Xaml of the window.resources as requested

  <Window.Resources>

    <Style x:Key="myLabelTemplate" TargetType="Label">
        <!--<Setter Property="Background" Value="DarkKhaki"></Setter>
         <Setter Property="Foreground" Value="White"></Setter>-->

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <Border>
                        <Grid>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                            </Grid.ColumnDefinitions>

                           <!-- <TextBlock Background="Green" Text="{Binding TableName}" Grid.Column="0"></TextBlock>
                            <TextBlock Background="Red" Text="{Binding ColumnName}"  Grid.Column="1"></TextBlock>-->

                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>


    <!-- <ControlTemplate TargetType="Label" x:Key="tablelLabel">
        <Grid>
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="5" CornerRadius="5">
                <Border.Background>
                    <SolidColorBrush Color="#50000000"></SolidColorBrush>
                </Border.Background>
            </Border>
            <Grid>
                <Grid.Background>
                    <LinearGradientBrush StartPoint="0.5,0"  EndPoint="0.5,1">
                        <GradientStop Color="Transparent" Offset="0" />
                        <GradientStop Color="Black" Offset="1" />
                        <GradientStop Color="Transparent" Offset="1" />
                    </LinearGradientBrush>
                </Grid.Background>
            </Grid>
        </Grid>

    </ControlTemplate> -->

    <ControlTemplate TargetType="Label" x:Key="generalLabel">
        <Grid>
            <Border BorderBrush="#34B7AEAE" BorderThickness="3" CornerRadius="5">
            </Border>
        </Grid>
    </ControlTemplate>

</Window.Resources>

You need to change the binding as follows:

<TextBlock Background="Green" Text="{Binding Source={StaticResource data}, Path=TableName}" Grid.Column="0"/>
 <TextBlock Background="Red" Text="{Binding Source={StaticResource data}, Path=ColumnName}"  Grid.Column="1"></TextBlock>

I tried the revised code with a simple class having the two properties and it worked successfully.

The ObjectDataProvider was referenced as follows:

 <ObjectDataProvider x:Key="data" ObjectType="{x:Type local:MyClass}">
 </ObjectDataProvider>

In this case I recommend you create custom control. It would be more elegant.

IMHO, the Label's DataContext is set wrong, Hence the absence of value being displayed.

My guess is that the default DataContext applies for the Label here, and it does not contain the TableName and ColumnName properties.

have a look at your output window and look for "error 40, Binding bla bla bla ....." this will give you more info on this.

beware, broken dataBindings will not always produce an exception, they sometimes will only show a warning in the output window.

you should give more info on your Label class and how you set the dataContext to the object containing the mentioned properties.

you're probably missing something like

MyCustomLabel.DataContext = MyCustomObjectCOntainingTheProperties;

somewhere

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