Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf DataTemplateSelector和Windows 8_Wpf_Xaml_Mvvm_Windows 8_Datatemplateselector - Fatal编程技术网

Wpf DataTemplateSelector和Windows 8

Wpf DataTemplateSelector和Windows 8,wpf,xaml,mvvm,windows-8,datatemplateselector,Wpf,Xaml,Mvvm,Windows 8,Datatemplateselector,我有一个很奇怪的问题。 我已经使用wpf、telerik(office2007主题)编写了应用程序。 我使用DataTemplateSelector为不同类型的模型动态创建视图。 在Windows7中一切都很好,但在Windows8中DataTemplateSelector不起作用! 世界跆拳道联盟? 你能帮我解决我的问题吗 public class TabDataTemplateSelector:DataTemplateSelector { public override DataTe

我有一个很奇怪的问题。 我已经使用wpf、telerik(office2007主题)编写了应用程序。 我使用DataTemplateSelector为不同类型的模型动态创建视图。 在Windows7中一切都很好,但在Windows8中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>
公共类选项卡DataTemplateSelector:DataTemplateSelector
{
公共覆盖数据模板SelectTemplate(对象项,DependencyObject容器)
{
if(item==null | | container==null)
返回null;
FrameworkElement=Application.Current.MainWindow作为FrameworkElement;
if(元素==null)
返回null;
if(项目为石化CatalogViewModel)
{
返回元素。FindResource(“PetrochemicalCatalogDataTemplate”)作为数据模板;
}
如果(项目为FuelPriceViewModel)
{
返回元素.FindResource(“FuelPriceDataTemplate”)作为DataTemplate;
}
if(项目为RoleViewModel)
{
返回元素.FindResource(“RoleDateTemplate”)作为数据模板;
}
如果(项目为IEquipmentViewModel)
{
返回元素.FindResource(“EquipmentDataTemplate”)作为DataTemplate;
}
if(项目为IZondsViewModel)
{
返回元素.FindResource(“ZondDataTemplate”)作为数据模板;
}
如果(项目为ITankCatalogViewModel)
{
返回元素FindResource(“TanksDataTemplate”)作为DataTemplate;
}
if(项目为IDispensersWorkplaceViewModel)
{
返回元素.FindResource(“DispensingDataTemplate”)作为DataTemplate;
}
if(项目为IDispensersetWorkplaceViewModel)
{
返回元素.FindResource(“DispenderSetWorkplaceTemplate”)作为数据模板;
}
如果(项目为IPaymentTypeCatalogViewModel)
{
返回元素.FindResource(“PaymentTypeWorkplaceTemplate”)作为数据模板;
}
如果(项目为ICashManagementViewModel)
{
返回元素.FindResource(“现金管理模板”)作为数据模板;
}
如果(项目为ITerminalWorkplaceViewModel)
{
返回元素.FindResource(“TerminalDataTemplate”)作为DataTemplate;
}
if(项目为IPriceingWorkplaceViewModel)
{
返回元素.FindResource(“PricingDataTemplate”)作为DataTemplate;
}
返回null;
}
}

执行所需操作的标准方法是将每个
数据模板
放入
用户控件
(没有
数据模板
对象,例如只添加内容),将它们命名为
…视图
(其中“…”来自相关的视图模型类)并使用
App.xaml
中定义的
DataTemplate
s将它们连接到视图模型:

<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>

...

执行所需操作的标准方法是将每个
数据模板
放入
用户控件
(没有
数据模板
对象,例如只添加内容),将它们命名为
…视图
(其中“…”来自相关的视图模型类)并使用
App.xaml
中定义的
DataTemplate
s将它们连接到视图模型:

<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>

...

与windows 7和windows 8有些不同。 我也有同样的问题,我发现DataType属性不支持windows 8中的DataTemplate,因此我必须根据它们的名称选择它,并使用自定义DataTemplateSelector。 下面是我的代码摘要,其中列表中填充了不同的类型:

XAML:


希望这能有所帮助。

与windows 7和windows 8有些不同。 我也有同样的问题,我发现DataType属性不支持windows 8中的DataTemplate,因此我必须根据它们的名称选择它,并使用自定义DataTemplateSelector。 下面是我的代码摘要,其中列表中填充了不同的类型:

XAML:


希望这能有所帮助。

如果您有根据,您现在可能已经得到了一些答案。有人知道这个问题吗?感谢您花时间改进您的问题。如果您有根据,你现在应该已经得到了一些答案。有人知道这个问题吗?谢谢你花时间改进你的问题。
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;
    }
}