0

我有一个很奇怪的问题。我已经使用 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>
4

2 回答 2

3

事情与 Windows 7 和 Windows 8 有点不同。我遇到了同样的问题,我发现 Windows 8 中的 DataTemplate 不支持 DataType 属性,所以我必须根据它们的名称选择它并使用自定义 DataTemplateSelector . 这是我的代码的摘录,其中一个列表填充了不同的类型:

XAML:

<UserControl.Resources>
    <s:CustomDataTemplateSelector
        x:Key="CustomDataTemplateSelector" />
    <DataTemplate
        x:Key="Activity1">
        <c:ActivityControl1 />
    </DataTemplate>
  <DataTemplate
        x:Key="Activity2">
        <c:ActivityControl2 />
  </DataTemplate>
</UserControl.Resources>

<Grid>
    <ListView
        FontSize="20"
        BorderThickness="0"
        Foreground="Black"
        ItemsSource="{Binding Activities}"
        HorizontalContentAlignment="Stretch"
        ItemTemplateSelector="{StaticResource CustomDataTemplateSelector}" />
</Grid>

CS:

public class CustomDataTemplateSelector : DataTemplateSelector
{
    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        string key = item.GetType().Name;
        FrameworkElement element = container as FrameworkElement;
        while (element != null)
        {
            if (element.Resources.ContainsKey(key))
                return element.Resources[key] as DataTemplate;
            element = VisualTreeHelper.GetParent(element) as FrameworkElement;
        }
        return Application.Current.Resources[key] as DataTemplate;
    }
}

希望这可以帮助。

于 2013-11-16T11:28:19.757 回答
1

做你想做的事的标准方法是将你的每个DataTemplates 放入 a UserControl(没有DataTemplate对象,例如,只需添加内容),命名它们...View(其中 '...' 来自相关的视图模型类)并连接使用以下DataTemplate定义的 s 将它们添加到您的视图模型App.xaml

<DataTemplate DataType="{x:Type ViewModels:PetrochemicalCatalogViewModel}">
    <Views:PetrochemicalCatalogView />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:FuelPriceViewModel}">
    <Views:FuelPriceView />
</DataTemplate>
...
<DataTemplate DataType="{x:Type ViewModels:IPriceingWorkplaceViewModel}">
    <Views:IPriceingWorkplaceView />
</DataTemplate>
于 2013-10-23T08:08:12.493 回答