2012-07-15 29 views
3

您好我想知道如何將我的MainWindow.xaml分成不同的XAML文件?我想將我的風格外包,並在需要時包括它們。我搜索了一些解決方案,發現了一些stackoverflow帖子,但沒有人幫助我。如何將MainWindow.xaml分割成單獨的文件? - 外包<Style>?

我想實現這樣的(僞)

<Window x:Class="MyApp.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:MyApp" 
    Title="MyApp" Height="350" Width="525"> 
<Window.Resources>  
    //Import external xaml file with textbox style here 
    //instead of: 
<Style TargetType="{x:Type TextBox}"> 
    <Style.Triggers> 
     //add code here 
    </Style.Triggers> 
</Style> 
</Window.Resources> 
<StackPanel> 
    <TextBox Width="60"/> 
    <Button Content="Button" Height="23" Name="button1" Width="75" /> 
</StackPanel> 

回答

10

創建XAML ResourceDictionary文件,包括他們是這樣的:

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="./Styles/TextBlockStyle.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
     <!-- other resources here --> 
    </ResourceDictionary> 
</Window.Resources> 
+0

嗯,我以前試過,但它確實不行。也許我應該給它第二次機會... – TorbenJ 2012-07-15 18:31:44

+0

好吧,它現在的作品。當我嘗試之前不知道究竟發生了什麼錯誤。謝謝! :) – TorbenJ 2012-07-15 18:37:20

+0

@TorbenJonas:不客氣,很高興它的工作:) – 2012-07-15 18:39:38

相關問題