2013-02-28 74 views
0

我正在使用我的第一个Windows 8 RT应用程序,并使用示例网格应用程序启动。我的目标是改变其中一个网格项目的风格。覆盖特定数据项的样式

我已经能够“发现”这个项目通过使用这样的:

if (dataItem.UniqueId.StartsWith("Group-1-Item-1")) 

然后我创造了这个风格在C#(就像一个例子);

Style style = new Style (typeof(SampleDataItem)); 
Thickness thick = new Thickness(4,4,4,4); 
style.Setters.Add(new Setter(Border.BorderThicknessProperty, new Thickness(100))); 

不过,现在我有这种风格应用到特定数据项 - 我已经尝试了很多东西,但我不把他们现在,我看他们太大的意义。

+0

您刚才是否正在做实验,或者您是否有一个特定的标准,即您打算将其用作具有不同风格的给定项目的基础? – devhammer 2013-03-01 01:34:33

回答

0

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

一旦这样做了,我可以运行该项目,这是我看到:

Grid App with Converter

希望有帮助!

有关Windows应用商店应用开发的更多信息,请注册Generation App

+0

非常感谢您的回答_devhammer_!它真的帮助了我,并且我能够按照你的建议来实现这一点。此外,它帮助我更好地了解数据绑定,这是我还没有理解的东西。 我剩下一个小问题,也许你也可以提供帮助。我的想法是将背景图像应用于某些图块,并且我可以通过使用目录链接而不是红色/绿色来实现。不幸的是,由于使用了.png/.jpg,平铺图像不是非常尖锐。是否可以使用xaml矢量形状作为背景图像?再次感谢你! – WalterB 2013-03-01 12:44:44

+0

很高兴我能帮到你。至于使用XAML形状作为背景,我认为你应该可以做到这一点......只需用嵌套在Border元素标签中的所需XAML替换嵌入式Image元素即可。试试看吧......我对XAML的态度并不像我在HTML上那么深,但我认为这应该起作用。祝你好运! – devhammer 2013-03-01 15:17:20