我有一个很奇怪的问题。我已经使用 wpf、telerik(office2007 主题)编写了应用程序。我使用 DataTemplateSelector 为不同类型的模型动态创建视图。在 Windows 7 中,一切都很好,但在 Windows 8 中 DataTemplateSelector 不起作用!怎么回事?你能帮我解决我的问题吗?
public class TabDataTemplateSelector:DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if(item==null || container == null)
return null;
FrameworkElement element = Application.Current.MainWindow as FrameworkElement;
if (element == null)
return null;
if (item is PetrochemicalCatalogViewModel)
{
return element.FindResource("PetrochemicalCatalogDataTemplate") as DataTemplate;
}
if (item is FuelPriceViewModel)
{
return element.FindResource("FuelPriceDataTemplate") as DataTemplate;
}
if (item is RoleViewModel)
{
return element.FindResource("RoleDataTemplate") as DataTemplate;
}
if (item is IEquipmentViewModel)
{
return element.FindResource("EquipmentDataTemplate") as DataTemplate;
}
if (item is IZondsViewModel)
{
return element.FindResource("ZondDataTemplate") as DataTemplate;
}
if (item is ITankCatalogViewModel)
{
return element.FindResource("TanksDataTemplate") as DataTemplate;
}
if (item is IDispensersWorkplaceViewModel)
{
return element.FindResource("DispensingDataTemplate") as DataTemplate;
}
if (item is IDispenserSetsWorkplaceViewModel)
{
return element.FindResource("DispenserSetsWorkplaceTemplate") as DataTemplate;
}
if (item is IPaymentTypeCatalogViewModel)
{
return element.FindResource("PaymentTypeWorkplaceTemplate") as DataTemplate;
}
if (item is ICashManagementViewModel)
{
return element.FindResource("CashManagementTemplate") as DataTemplate;
}
if (item is ITerminalWorkplaceViewModel)
{
return element.FindResource("TerminalDataTemplate") as DataTemplate;
}
if (item is IPriceingWorkplaceViewModel)
{
return element.FindResource("PriceingDataTemplate") as DataTemplate;
}
return null;
}
}
<local:TabDataTemplateSelector x:Key="tabDataTemplateSelector"/>
<DataTemplate x:Key="TabContentTemplate">
<telerik:RadBusyIndicator x:Name="rbiBusy" VerticalAlignment="Stretch" IsBusy="{Binding IsBusy}" HorizontalContentAlignment="Stretch" telerik:StyleManager.Theme="Windows8Touch" VerticalContentAlignment="Stretch">
<ContentPresenter ContentTemplateSelector="{StaticResource tabDataTemplateSelector}" Content="{Binding TabContentViewModel}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</telerik:RadBusyIndicator>
</DataTemplate>