我在Windows 8的winrt应用程序中遇到ContentTemplateSelector的一些问题。在左边我可以选择一个项目,在中心区域我想显示它的内容。但内容类型不同。所以我决定使用DataTemplateSelector。但它没有工作。
我认为问题是一些错误的绑定,因为SelectTemplateCore Methode项目对象始终为null。
数据类:
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>
答案 0 :(得分:0)
将ContenControlComponente更改为:
<ContentControl Grid.Row="2" Grid.ColumnSpan="2" Margin="0,20,0,0"
Name="contentControl"
Content="{Binding SelectedItem, ElementName=itemListView}"
ContentTemplateSelector="{StaticResource MyDataTemplateSelector}" />
它正在运作。