在StandardStyles.xaml中找到Grid App项目模板的dataItems的数据模板,并通过键“Standard250x250ItemTemplate”引用。
如果想法是根据项目的内容改变样式,一种方法可能是使用Binding.Converter,as shown here将所讨论内容的值转换为所需的样式或样式属性。
例如,如果我想让默认网格应用程序模板中的任何项目在项目编号小于或等于3时具有绿色背景,并且如果项目编号小于或等于3则为红色,我会创建以下转换器类(在我的情况下,我刚添加的类到GroupedItemsPage.xaml.cs,只是GroupedItemsPage部分类前):
// Custom class implements the IValueConverter interface.
public class StyleConverter : IValueConverter
{
#region IValueConverter Members
// Define the Convert method to change a title ending with > 3
// to a red background, otherwise, green.
public object Convert(object value, Type targetType,
object parameter, string language)
{
// The value parameter is the data from the source object.
string title = (string)value;
int lastNum = (int.Parse(title.Substring(title.Length - 1)));
if (lastNum > 3)
{
return "Red";
}
else
{
return "Green";
}
}
// ConvertBack is not implemented for a OneWay binding.
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
throw new NotImplementedException();
}
#endregion
}
一旦类被定义,将具有所需Convert方法以来,我添加一个实例在我的App.xaml文件中,因此它可用于StandardStyles.xaml中的项目模板:
<!-- Application-specific resources -->
<local:StyleConverter x:Key="StyleConverter"/>
在StandardStyles.xaml,我做了从项目的Title属性结合,我的转换器类,像这样的Standard250x250ItemTemplate的副本:
<DataTemplate x:Key="Standard250x250ItemTemplate_Convert">
<Grid HorizontalAlignment="Left" Width="250" Height="250">
<Border Background="{Binding Title,
Converter={StaticResource StyleConverter}}">
</Border>
<StackPanel VerticalAlignment="Bottom"
Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Text="{Binding Title}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="60" Margin="15,0,15,0"/>
<TextBlock Text="{Binding Subtitle}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
</StackPanel>
</Grid>
</DataTemplate>
的关键部分是边界元素的结合背景属性标题,与转换器= {StaticResource StyleConverter},将项目模板连接到我的转换器。请注意,除了绑定Background属性之外,我还从项目模板的原始版本中删除了嵌套的Image元素。
最后,在GroupedItemsPage.xaml,我改变了项目模板,我的定制版本:
<!-- Horizontal scrolling grid used in most view states -->
<GridView
x:Name="itemGridView"
...
Padding="116,137,40,46"
ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
ItemTemplate="{StaticResource Standard250x250ItemTemplate_Convert}"
...
ItemClick="ItemView_ItemClick">
一旦这样做了,我可以运行该项目,这是我看到:
希望有帮助!
有关Windows应用商店应用开发的更多信息,请注册Generation App。
您刚才是否正在做实验,或者您是否有一个特定的标准,即您打算将其用作具有不同风格的给定项目的基础? – devhammer 2013-03-01 01:34:33