【问题标题】:DataTemplateSelector don't show contentDataTemplateSelector 不显示内容
【发布时间】:2014-08-20 15:30:12
【问题描述】:

我在 windows 8 的 winrt 应用程序中的 ContentTemplateSelector 有一些问题。在左侧我可以选择一个项目,在中心区域我想显示其中的内容。但内容类型不同。所以我决定使用 DataTemplateSelector。但它不起作用。

我认为问题在于某些错误的绑定,因为 SelectTemplateCore Methode 项目对象始终为空。

数据类:

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

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 代码:

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

内容区:

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

【问题讨论】:

  • 如果我将 contentControl DataContext="{StaticResource itemViewSource}" 更改为 Content="{StaticResource itemViewSource}" 我得到一个例外:WinRT information: Failed to assign to property 'Windows.UI.Xaml.Controls.ContentControl.Content'

标签: c# xaml windows-runtime winrt-xaml


【解决方案1】:

将 ContentControlComponente 更改为:

    <ContentControl Grid.Row="2" Grid.ColumnSpan="2" Margin="0,20,0,0"
         Name="contentControl" 
         Content="{Binding SelectedItem, ElementName=itemListView}"
         ContentTemplateSelector="{StaticResource MyDataTemplateSelector}" />

它正在工作。

【讨论】:

  • 我们能让模板选择器工作吗?我无法在 SelectTemplatecCore 方法上进行事件中断。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 2012-09-24
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
相关资源
最近更新 更多