如何将MainWindow.xaml拆分为单独的文件? - 外包<style>?</style>

时间:2012-07-15 18:22:52

标签: c# wpf xaml

您好,我想知道如何将MainWindow.xaml分成不同的xaml文件?我想把我的款式外包,并在我需要的时候包括它们。我搜索了一些解决方案,发现了一些stackoverflow帖子,但没有一个帮助我。

我想实现类似的东西(伪代码)

<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>

1 个答案:

答案 0 :(得分:10)

创建XAML ResourceDictionary文件,包括它们:

<Window.Resources>
    <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
              <ResourceDictionary Source="./Styles/TextBlockStyle.xaml" />
         </ResourceDictionary.MergedDictionaries>
         <!-- other resources here -->
    </ResourceDictionary>
</Window.Resources>
相关问题