   [.net] [WPF/MVVM] Problem having a user control as a DataTemplate - nothing is showing
  edit: I've now fixed this.  The problem was that a new ViewModel was being created when the View was being created, so it would retrieve the values of the ViewModel just created, rather than the one in the list... I'm using MVVM to create a program with contacts and backed up messages to/from each contact.  I have my MainView which contains a ListBox, and in this ListBox I want all of the contacts.  I've read that you can set the DataTemplate of the ListBox to a user control (in my case ContactView).  ContactView is fairly simple at the moment, but I will very likely expand on it later:<Border>   <StackPanel DockPanel.Dock="Right" Orientation="Vertical">  <TextBlock Text="{Binding ContactFirstName}" FontWeight="Bold" Margin="5, 0, 5, 0"></TextBlock>  <TextBlock Text="{Binding ContactLastName, diagnostics:PresentationTraceSources.TraceLevel=High}" FontWeight="Bold" Margin="5, 0, 5, 0"></TextBlock>  <TextBlock Text="{Binding ContactNumber}" Margin="5, 0, 5, 0"></TextBlock>   </StackPanel>    </Border>The code in MainView for the ListBox is:<Border> <ListBox ItemsSource="{Binding Path=ContactList, Mode=TwoWay}" Grid.RowSpan="2"><ListBox.ItemTemplate>    <DataTemplate>   <View:ContactView />    </DataTemplate></ListBox.ItemTemplate> </ListBox>  </Border>I have correctly set the DataContext in the code-behind of the ContactView to ContactViewModel.  I have also tried setting the DataContext in the XAML in MainView where I actually call the user control.So my problem is that nothing is showing visually.  The binding expressions just aren't working.  I found a website that gave some descriptions of debugging bindings, and I used a converter to check, and they came back null.  I have also used tracing, which came back as follows:System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ContactFirstName; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')System.Windows.Data Warning: 54 : Created BindingExpression (hash=11673710) for Binding (hash=38404320)System.Windows.Data Warning: 56 :   Path: 'ContactLastName'System.Windows.Data Warning: 58 : BindingExpression (hash=11673710): Default mode resolved to OneWaySystem.Windows.Data Warning: 59 : BindingExpression (hash=11673710): Default update trigger resolved to PropertyChangedSystem.Windows.Data Warning: 60 : BindingExpression (hash=11673710): Attach to System.Windows.Controls.TextBlock.Text (hash=9400000)System.Windows.Data Warning: 65 : BindingExpression (hash=11673710): Resolving source System.Windows.Data Warning: 68 : BindingExpression (hash=11673710): Found data context element: TextBlock (hash=9400000) (OK)System.Windows.Data Warning: 69 : BindingExpression (hash=11673710): DataContext is nullSystem.Windows.Data Warning: 63 : BindingExpression (hash=11673710): Resolve source deferredSystem.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ContactLastName; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ContactNumber; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')System.Windows.Data Warning: 65 : BindingExpression (hash=11673710): Resolving source System.Windows.Data Warning: 68 : BindingExpression (hash=11673710): Found data context element: TextBlock (hash=9400000) (OK)System.Windows.Data Warning: 76 : BindingExpression (hash=11673710): Activate with root item ContactViewModel (hash=56738739)System.Windows.Data Warning: 106 : BindingExpression (hash=11673710):   At level 0 - for ContactViewModel.ContactLastName found accessor RuntimePropertyInfo(ContactLastName)System.Windows.Data Warning: 102 : BindingExpression (hash=11673710): Replace item at level 0 with ContactViewModel (hash=56738739), using accessor RuntimePropertyInfo(ContactLastName)System.Windows.Data Warning: 99 : BindingExpression (hash=11673710): GetValue at level 0 from ContactViewModel (hash=56738739) using RuntimePropertyInfo(ContactLastName): <null>System.Windows.Data Warning: 78 : BindingExpression (hash=11673710): TransferValue - got raw value <null>System.Windows.Data Warning: 87 : BindingExpression (hash=11673710): TransferValue - using final value <null>Any ideas why this isn't working? Thanks.[Edited by - Side Winder on June 1, 2010 8:49:06 AM]