[英]DataTemplateSelector don't show content
I have some problems with ContentTemplateSelector in a winrt app for windows 8. In the left I can select a item and in the center area I want to display the content from it. 我在Windows 8的Winrt应用程序中使用ContentTemplateSelector遇到一些问题。在左侧,我可以选择一个项目,在中间区域,我想显示其中的内容。 But the content types are different.
但是内容类型不同。 So I decided to use an DataTemplateSelector.
因此,我决定使用DataTemplateSelector。 But it dosen't work.
但这行不通。
I think the problem is some wrong binding because the SelectTemplateCore Methode item object is always null. 我认为问题是绑定错误,因为SelectTemplateCore Methode项目对象始终为null。
Dataclasses: 资料类别:
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 class: 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 Code: 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>
The Content area: 内容区域:
<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>
After Change the ContenControlComponente to: 将ContenControlComponente更改为:
<ContentControl Grid.Row="2" Grid.ColumnSpan="2" Margin="0,20,0,0"
Name="contentControl"
Content="{Binding SelectedItem, ElementName=itemListView}"
ContentTemplateSelector="{StaticResource MyDataTemplateSelector}" />
it is working. 这是工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.