首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataTemplateSelector不显示内容

DataTemplateSelector不显示内容
EN

Stack Overflow用户
提问于 2014-08-19 09:30:50
回答 1查看 235关注 0票数 0

在windows 8的winrt应用程序中,我遇到了一些ContentTemplateSelector问题。在左边,我可以选择一个项目,而在中心区域,我想要显示其中的内容。但内容类型不同。所以我决定使用DataTemplateSelector。但这不管用。

我认为问题在于绑定错误,因为SelectTemplateCore卫理德项对象总是为空。

数据:

代码语言:javascript
复制
public class DataItem {
    public DataItem(String uniqueId, String title, String subtitle, String imagePath, String description, String content) {
        this.UniqueId = uniqueId;
        this.Title = title;
        this.Subtitle = subtitle;
        this.Description = description;
        this.ImagePath = imagePath;
        this.Content = content;
    }

    public string UniqueId { get;  set; }
    public string Title { get;  set; }
    public string Subtitle { get;  set; }
    public string Description { get;  set; }
    public string ImagePath { get;  set; }
    public string Content { get;  set; }

    public override string ToString() {
        return this.Title;
    }
}

public class TextDataItem : DataItem {
    public TextDataItem(string p1, string p2, string p3, string p4, string p5, string p6) : base (p1, p2, p3, p4, p5, p6) {

    }
}

public class ImageDataItem : DataItem {
    public ImageDataItem(string p1, string p2, string p3, string p4, string p5, string p6): base(p1, p2, p3, p4, p5, p6) {
    }
}

DataTemplateSelector类:

代码语言:javascript
复制
public class MyDataTemplateSelector : DataTemplateSelector {
    public DataTemplate TextTemplate { get; set; }
    public DataTemplate ImageTemplate { get; set; }

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) {

        if (item == null) {
            return null;
        }

        if (item is TextDataItem)
            return TextTemplate;
        if (item is ImageDataItem)
            return ImageTemplate;

        return base.SelectTemplateCore(item, container);
    }
}

XAML代码:

代码语言:javascript
复制
<Page.Resources>
    <CollectionViewSource
        x:Name="itemsViewSource"
        Source="{Binding Items}"
        d:Source="{Binding Groups[0].Items}"/>

    <CollectionViewSource
        x:Name="itemViewSource"
        Source="{Binding Items}"
        d:Source="{Binding SelectedItem, ElementName=itemListView}" />

    <DataTemplate x:Key="TextDataTemplate">
        <Grid HorizontalAlignment="Right" Width="400" Height="280">
            <StackPanel>
                <TextBlock Text="12345" Margin="15,0,15,20"/>
                <TextBlock Text="{Binding Content}" Margin="15,0,15,0">
            </StackPanel>
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="ImageDataTemplate">
        <Grid HorizontalAlignment="Left" Width="400" Height="280">
            <StackPanel VerticalAlignment="Bottom">
                <Image Source="{Binding Content}" Stretch="UniformToFill"/>
            </StackPanel>
        </Grid>
    </DataTemplate>

    <data:MyDataTemplateSelector x:Key="MyDataTemplateSelector"
        TextTemplate="{StaticResource TextDataTemplate}"
        ImageTemplate="{StaticResource ImageDataTemplate}">
    </data:MyDataTemplateSelector>

</Page.Resources>

内容领域:

代码语言:javascript
复制
<ScrollViewer
        x:Name="itemDetail"
        DataContext="{StaticResource itemViewSource}">

        <Grid x:Name="itemDetailGrid" Margin="0,60,0,50" >
            <Grid.RowDefinitions>
                <RowDefinition Height="75"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <ContentControl Grid.Row="2" Grid.ColumnSpan="2" Margin="0,20,0,0"
                     Name="contentControl" 
                     DataContext="{StaticResource itemViewSource}"
                     ContentTemplateSelector="{StaticResource MyDataTemplateSelector}" />      
        </Grid>
   </ScrollViewer>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-20 07:30:12

将ContenControlComponente更改为:

代码语言:javascript
复制
    <ContentControl Grid.Row="2" Grid.ColumnSpan="2" Margin="0,20,0,0"
         Name="contentControl" 
         Content="{Binding SelectedItem, ElementName=itemListView}"
         ContentTemplateSelector="{StaticResource MyDataTemplateSelector}" />

起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25379987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档