2015-12-29 31 views
1

我有一些包含在资源引用页面中的样式。WinRT XAML在DataTemplate中应用样式

<ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="MyStyles.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

我为TextBlockButton等命名样式当使用它们时,一切工作正常。

当我尝试在ItemsControlDataTemplate内使用它们时,它们不会被应用。

<ItemsControl> 
    <ItemsControl.ItemsPanel> 
     <StackPanel Orientation="Horizontal" /> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="blah" Style="{StaticResource MyTextBlockStyle}" /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

如何我可以得到从另一个文件包含的命名风格在里面工作我DataTemplate像它在其他地方在网页上?

回答

0

而不是将样式包含在特定页面的XAML中,请将其包含在App.xaml中。下面是如何将它们添加

<Application x:Class="MyApp.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="using:MyApp"> 

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Resources/Resources.xaml"></ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

我使用Resources.xaml定义风格DataTemplate所有的时间。

+0

我的所有代码都在几个项目使用的另一个程序集中。在另一个程序集中为单个页面设置应用程序设置xaml似乎并不合理。 –

+0

谈到不同的集会,你尝试[这](http://blogs.msdn.com/b/madenwal/archive/2015/02/09/how-to-link-style-resource-dictionaries-located-in - 不同的组件功能于winrt.aspx)? –

+0

我有'Shared.dll'。这个程序集包含'Page.xaml'和'MyStyles.xaml'。还有另一个应用程序包含此程序集。它适用于页面上的所有内容,直到ItemsControl的'DataTemplate'内部。 –

0

除了手动定义外,您还可以使用混合通过设计面板。

当你右击你的

的GridView>编辑其他模板>编辑生成项目

enter image description here

您可以编辑您的内容。然后你可以选择任何你的项目,并定义它们的设计窗格样式像下面

enter image description here

如果您没有看到您的按钮,文本块样式右键菜单,你可以检查这个帖子让他们重复使用 Style only works for the first occurence when outside Grid.Resources?

希望这会有所帮助。

+0

我没有安装Blend。如果需要更改,我从SDK中获得'generic.xaml'和'themeresources.xaml'来复制原始XAML。 –