views:

366

answers:

3

I have the following DataTemplate:

<DataTemplate DataType="{x:Type Client:WorkItem}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Label Name="lblIDText" Margin="2">WI ID:</Label>
        <Label Name="lblID" Margin="2" Grid.Column="1" 
               Target="{Binding Id}"></Label>

        <Label Name="lblTitleText" Grid.Row="1" Margin="2">WI Title:</Label>
        <Label Name="lblTitle" Margin="2" Grid.Row="2" Grid.ColumnSpan="2" 
               Target="{Binding Title}"></Label>

    </Grid>
</DataTemplate>

in my <Window.Resources> section. It is meant to show the Id and Title of a WorkItem object (from namespace Microsoft.TeamFoundation.WorkItemTracking.Client.)

I try to put this in a TabItem in a TabControl and it only shows the static text. (The WorkItem ID and title do not show, but the static text in my template does.)

Clearly the template is being fired, but the binding is not working and I can't seem to see why.

Here is my C# that calls it:

    private void PickWorkItem_Click(object sender, RoutedEventArgs e)
    {
        int Id = int.Parse(((Button) e.OriginalSource).Tag.ToString());

        _mediator.PickedWorkItem = GetWorkItemInQueryResultListByID(Id);
        tabAddLinks.DataContext = _mediator.PickedWorkItem;
        tabAddLinks.Content = _mediator.PickedWorkItem;

    }

I tried it with DataContext in and out and it works the same. When I debug, the value for _mediator.PickedWorkItem is set correctly (Id and Title are both fine).

Any ideas on how to fix this would be appreciated.

+1  A: 

You're binding the Label's Target property, when you actually want to be binding it's Content property:

<Label Content="{Binding Id}"/>

Also, consider using a TextBlock instead of a Label if you don't need the extra functionality a Label provides:

<TextBlock Text="{Binding Id}"/>

HTH,
Kent

Kent Boogaart
+1  A: 

I'm pretty new to WPF, so excuse me if I'm off base, but don't you need to set the Content property of the label, not the Target?

Adam Robinson
+2  A: 

You are binding Label.Target. Target is the UIElement being labelled. You need to bind Label.Content, or change it to a TextBlock and bind TextBlock.Text.

(My guess would be that you were trying to bind a non-existent Label.Text property and Intellisense helpfully chose Target for you instead...!)

itowlson