Why is WPF not picking the correct DataTemplate in my case?
I have 4 classes in my ViewModel that implements the IPage interface (the interface I created). In the ViewModel of my application, I have a CurrentPage property of type IPage.
In the View layer of my application, I have a ContentControl whose content is bound to the CurrentPage property.
So when I use the following code WPF won't find the correct DataTemplate
<UserControl ...>
<ContentControl Content="{Binding CurrentPage, Mode=OneWay}"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type operation:OperationListPageVM}">
<pages1:OperationListPageV/>
</DataTemplate>
<DataTemplate DataType="{x:Type operation:OperationDetailPageVM}">
<pages1:OperationDetailPageV/>
</DataTemplate>
<DataTemplate DataType="{x:Type part:PartListPageVM}">
<pages1:PartListPageV/>
</DataTemplate>
<DataTemplate DataType="{x:Type part:PartDetailPageVM}">
<pages1:PartDetailPageV/>
</DataTemplate>
<DataTemplate DataType="{x:Type pages:SettingsPageVM}">
<pages1:SettingsPageV/>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</UserControl>
But if I only supply one DataTemplate as ContentControl.ContentTemplate, it will apply it correctly.
<UserControl ...>
<ContentControl Content="{Binding CurrentPage, Mode=OneWay}"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<ContentControl.ContentTemplate>
<DataTemplate DataType="{x:Type operation:OperationListPageVM}">
<pages1:OperationListPageV/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</UserControl>
If I return these DataTemplates as resources, give them a key and create a DataTemplateSelector and add
ContentTemplateSelector="{StaticResource PageTemplateSelector}"
In a ControlTemplate using the DataTemplateSelector as follows:
using System.Windows;
using System.Windows.Controls;
using MaintenanceModuleV3.ViewModel;
using MaintenanceModuleV3.ViewModel.Pages;
using MaintenanceModuleV3.ViewModel.Pages.Operation;
using MaintenanceModuleV3.ViewModel.Pages.Part;
namespace MaintenanceModuleV3.View.TemplateSelector {
public class PageTemplateSelector : DataTemplateSelector {
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
if (AppVM.Instance.CurrentPage != null) {
if (AppVM.Instance.CurrentPage is OperationListPageVM) {
return (container as FrameworkElement).FindResource("OperationListPageTemplate") as DataTemplate;
} else if (AppVM.Instance.CurrentPage is OperationDetailPageVM) {
return (container as FrameworkElement).FindResource("OperationDetailPageTemplate") as DataTemplate;
} else if (AppVM.Instance.CurrentPage is PartListPageVM) {
return (container as FrameworkElement).FindResource("PartListPageTemplate") as DataTemplate;
} else if (AppVM.Instance.CurrentPage is PartDetailPageVM) {
return (container as FrameworkElement).FindResource("PartDetailPageTemplate") as DataTemplate;
} else if (AppVM.Instance.CurrentPage is SettingsPageVM) {
return (container as FrameworkElement).FindResource("SettingsPageTemplate") as DataTemplate;
}
}
return null;
}
}
}
Then the correct DataTemplate is selected.
Why won't WPF pick the correct DataTemplate if I put DataTemplates on the element resources in this case?
thank
NB: Using Framework 3.5
EDIT: I noticed a second problem: DataContext inside DataTemplates is NULL. Why is this? Why won't they use CurrentPage as DataContext? This is the case when I directly set the ContentTemplate. WPF doesn't seem to like bindings to typed properties of the interface.
EDIT 2: If I set DataContext OperListPageV explicitly in my constructor and use TemplateSelector everything works, but why should I use them?
EDIT 3: Full ViewModel code, if it helps:
App ViewModel
public class AppVM : INotifyPropertyChanged {
#region constructor
private AppVM() {
AppM.LanguageChanged += (sender, args) => {
onPropertyChanged("CurrentLanguage");
};
AppM.NbDaysAvgChanged += (sender, args) => {
onPropertyChanged("NbDaysAvg");
};
AppM.NbDaysMenuNotificationChanged += (sender, args) => {
onPropertyChanged("NbDaysMenuNotification");
};
}
#endregion
#region business
#endregion
#region events
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void onPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region properties
private static AppVM _instance;
public static AppVM Instance {
get { return _instance ?? (_instance = new AppVM()); }
}
#region notifypropertychanged
private OperationListPageVM _currentPage = OperationListPageVM.Instance;
public OperationListPageVM CurrentPage {
get { return _currentPage; }
set {
_currentPage = value;
onPropertyChanged("CurrentPage");
}
}
public Language CurrentLanguage {
get { return AppM.CurrentLanguage; }
}
public int NbDaysMenuNotification {
get { return AppM.NbDaysMenuNotification; }
}
public int NbDaysAvg {
get { return AppM.NbDaysAvg; }
}
public Dictionary<String, String> AvailableTranslations {
get { return AppM.AvailableTranslations; }
}
#endregion
#region commands
#endregion
#endregion
}
The page where I was supposed to display the ViewModel
public sealed class OperationListPageVM : INotifyPropertyChanged, IPage {
#region constructor
private OperationListPageVM() {
//fill the available operations with data stored in the App Model coming from the DataBase
foreach (MaintenanceOperation operation in AppM.Operations) {
AvailableOperations.Add(new MaintenanceOperationVM(operation));
}
foreach (MaintenanceOperationVM operation in AvailableOperations) {
DisplayedOperations.Add(operation);
}
}
#endregion
#region business
#endregion
#region events
public event PropertyChangedEventHandler PropertyChanged;
private void onPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region properties
private static OperationListPageVM _instance;
public static OperationListPageVM Instance {
get { return _instance ?? (_instance = new OperationListPageVM()); }
}
#region notifypropertychanged
private ObservableCollection<MaintenanceOperationVM> _displayedOperations = new ObservableCollection<MaintenanceOperationVM>();
public ObservableCollection<MaintenanceOperationVM> DisplayedOperations {
get { return _displayedOperations; }
set {
_displayedOperations = value;
onPropertyChanged("DisplayedOperations");
}
}
private readonly List<MaintenanceOperationVM> _availableOperations = new List<MaintenanceOperationVM>();
internal List<MaintenanceOperationVM> AvailableOperations {
get { return _availableOperations; }
}
#endregion
#region commands
#endregion
#endregion
}
The display level of the page I was supposed to display (OperationListPageV)
<UserControl x:Class="MaintenanceModuleV3.View.Pages.OperationListPageV"
...>
<touch:ScrollFriction2>
<ListBox ItemsSource="{Binding DisplayedOperations}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type viewModel:MaintenanceOperationVM}">
<view:MaintenanceOperationV/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</touch:ScrollFriction2>
</UserControl>
If you need more code, just ask for it. Thank!
source to share