0

I have a list of employees entities, bound to a listbox that implements a DataTemplate.

DataTemplate:

<DataTemplate x:Key="EmployeeTemplate">
 <Grid>
  <Grid.ColumnDefinitions>
   <ColumnDefinition Width="*"/>
   <ColumnDefinition Width="*"/>
  </Grid.ColumnDefinitions>
  <StackPanel Grid.Column="0" Orientation="Vertical" 
              VerticalAlignment="Top" Margin="2">
   <TextBlock Foreground="Black" FontSize="12"
              VerticalAlignment="Bottom" Text="{Binding Path=Test}">
   </TextBlock>
   <TextBlock Foreground="Black" FontSize="12"
              VerticalAlignment="Bottom" Text="{Binding Path=Language.ContactNumber}">
   </TextBlock>
  </StackPanel>
  <StackPanel Grid.Column="1" Orientation="Vertical" 
              VerticalAlignment="Top" Margin="2">
   <TextBlock Foreground="Black" FontSize="12"
              VerticalAlignment="Bottom" Text="{Binding Path=IdNumber}">
   </TextBlock>
   <TextBlock Foreground="Black" FontSize="12"
              VerticalAlignment="Bottom" Text="{Binding Path=ContactNumber}">
   </TextBlock>
  </StackPanel>
 </Grid>
</DataTemplate>

Listbox:

<ListBox ItemsSource="{Binding Path=Employees}"  
         x:Name="ListBoxEmployees" 
         ItemTemplate="{DynamicResource EmployeeTemplate}" 
         BorderBrush="DarkGray" 
         Margin="5"/>

My Datacontext is a viewModel called EmployeeViewModel, it contains the collection of employees. This binding works fine, the employees gets displayed and all is good. The Problem is that the EmployeeViewModel inherits from a base abstract ViewModel that contains a static property called Language. This model has various fields that I bind labels to all over the app. The values in the data template does not work. Why?

Additional info: This listbox is in a usercontrol on the mainwindow.xaml

Edit:

 xmlns:viewModels="clr-namespace:POC.DesktopClient.ViewModels"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance viewModels:EmployeeViewModel}"

These XML namespaces at the top of my usercontrol allows xaml intelisense when binding. The intelisence does pick up the language object and its fields within.

Swifty
  • 1,422
  • 2
  • 18
  • 38
  • Maybe you sohuld bind to static resource, check this http://stackoverflow.com/a/3862828/67505 – Chen Kinnrot Mar 03 '14 at 12:10
  • try to bind like "{Binding Source={StaticResource mvvm},Path=SelectedItem}" – Nomi Ali Mar 03 '14 at 12:12
  • @Nomi that produces this error: "The resource "Language" could not be resolved." – Swifty Mar 03 '14 at 12:20
  • @Swifty you must add a reference of abstract view model class above and the set the language class object in in xaml then you can resolve this error. – Nomi Ali Mar 03 '14 at 12:30
  • I suppose I should have added that I'm new to WPF & MVVM, how would I go about doing that? – Swifty Mar 03 '14 at 12:32
  • http://stackoverflow.com/questions/936304/binding-to-static-property – Nomi Ali Mar 03 '14 at 12:40
  • That works, thanks. But the textblocks does not update with the rest of the textblocks in the app when I change the values of the language entity – Swifty Mar 03 '14 at 12:50
  • @swifty you must Implement your entity class from INotifyPropertyChanged interface and then on setter property call the this.OnPropertyChanged("your property name") method. – Nomi Ali Mar 03 '14 at 13:08
  • It does. When I change the language object, all items bound to it changes, except for those in the DataTemplate of the Listbox. – Swifty Mar 03 '14 at 13:17

1 Answers1

2

As you stated Language property lies in ViewModel class. But having it in DataTemplate, will make it to search for in Employee class and not in your ViewModel.

You need to access ListBox's DataContext which you can do using RelativeSource:

<TextBlock Text="{Binding Path=DataContext.Language.ContactNumber,
      RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"/>

UPDATE

For comment:

Binding resolves correctly using this, but it still does not update when I change the language model.

First of all for instance properties to be updated on GUI you need to implement INotifyPropertyChanged on your ViewModel class.

But however, this won't work for static properties. For static properties to be updated on GUI, you have to rebind them till WPF 4.5. If you are using WPF 4.5, you can do that using StaticPropertyChanged. Please refer to the details here WPF 4.5 binding and change notification for static properties.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Binding resolves correctly using this, but it still does not update when I change the language model. – Swifty Mar 03 '14 at 18:14
  • So apparently I'm running WPF v3.0. How do I update to the latest version? Why isn't it bundled with VS2013 ultimate? – Swifty Mar 04 '14 at 09:10
  • Like I mentioned in earlier versions you have to manually update the GUI component OR you have to make it instance property. – Rohit Vats Mar 04 '14 at 09:18
  • Could you please guide me with the manual option, since the instance property seems to only be possible with WPF4.5 – Swifty Mar 04 '14 at 09:40