WinRT XAML在DataTemplate中应用样式

时间:2015-12-29 06:16:41

标签: xaml windows-runtime winrt-xaml

我有一些样式包含在带有资源引用的页面中。

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

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

当我尝试在DataTemplate ItemsControl内使用它们时,它们无法应用。

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

如何让其他文件中包含的命名样式在我的DataTemplate内工作,就像在页面上的其他位置一样?

2 个答案:

答案 0 :(得分: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中定义的样式。

答案 1 :(得分:0)

不是手动定义,而是可以通过设计面板使用Blend。

右键点击

  

Gridview&gt;编辑其他模板&gt;编辑生成的项目

enter image description here

您可以编辑自己的内容。然后,您可以选择任何项目并在设计窗格上定义其样式,如下所示

enter image description here

如果在右键菜单中没有看到按钮,文本块样式,则可以查看此帖子以使其可重复使用 Style only works for the first occurence when outside Grid.Resources?

希望这会有所帮助。

相关问题