How to have a user control as a ListBoxItem

Posted by Harry on Stack Overflow See other posts from Stack Overflow or by Harry
Published on 2010-05-31T22:30:29Z Indexed on 2010/05/31 22:33 UTC
Read the original article Hit count: 203

Filed under:
|
|
|

I want to bind a user control (View) to a ListBoxItem. The ListBox is bound to a collection of ViewModels. I have set the ListBox's ItemTemplate as so:

<ListBox.ItemTemplate>
     <DataTemplate>
           <View:ContactView/>     
     </DataTemplate>
 </ListBox.ItemTemplate>

But all I get are blank ListBoxItems. I can click on them, but nothing is showing visually. My ContactView code is very simply:

<Border>
    <DockPanel>
        <StackPanel DockPanel.Dock="Right" Orientation="Vertical">
            <TextBlock Text="{Binding Path=ContactFirstName, FallbackValue=FirstName}" FontWeight="Bold" Margin="5, 0, 5, 0"></TextBlock>
            <TextBlock Text="{Binding Path=ContactLastName, FallbackValue=LastName}" FontWeight="Bold" Margin="5, 0, 5, 0"></TextBlock>
            <TextBlock Text="{Binding Path=ContactNumber, FallbackValue=Number}" Margin="5, 0, 5, 0"></TextBlock>
        </StackPanel>
    </DockPanel>
</Border>

What could be wrong with this? Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET