2015-12-29 70 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。 –