2012-07-15 41 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