Question

I basically have this situation:

<Style x:Key="MyListBoxItem" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Grid>
                    <ContentControl Content="{TemplateBinding Content}">
                        <!-- style stuff-->
                    </ContentControl>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
 </Style>

and this ListBox in a separate file

<ListBox ItemsSource="{Binding Path=Users}" ItemsContainerStyle="{DynamicResource MyListBoxItem}">
    <ListBox.ItemTemplate>
         <DataTemplate>
              <TextBlock Text="{Binding Path=Id}"/>
         </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

But when we run it, the listbox only shows .ToString(). It does not bind properly. How do I get the ItemTemplate to hook up properly with the contentcontrol content property?

I have a suspicion I might be doing something very wrong.

Was it helpful?

Solution

In the ListBoxItem style add ContentTemplate="{TemplateBinding ContentTemplate}"

<ContentControl Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}">
                                    <!-- style stuff-->
                                </ContentControl>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top