如何将MainWindow.xaml拆分为多个文件? - 外包<Style>?

3

您好,我想知道如何将我的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个回答

10

创建 XAML ResourceDictionary 文件,按照以下方式包含它们:

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

嗯,我之前尝试过这个,但是没有成功。也许我应该再给它一次机会... - TorbenJ
好的,现在它可以运行了。之前我不知道出了什么问题。谢谢! :) - TorbenJ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接