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.

有帮助吗?

解决方案

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

<ContentControl Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}">
                                    <!-- style stuff-->
                                </ContentControl>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top