Listbox DataTemplate field binding isn't worki

2019-08-20 04:14发布

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.

标签: wpf mvvm
1条回答
不美不萌又怎样
2楼-- · 2019-08-20 04:48

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.

查看更多
登录 后发表回答