Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split MainWindow.xaml into separate files? - Outsourcing of <Style>?

Tags:

c#

wpf

xaml

Hi I would like to know how i can separate my MainWindow.xaml into different xaml files? I would like to outsource my styles and include them whenever i need them. I searched for some solutions and found some stackoverflow posts but none of them helped me.

I would like to achieve something like this (pseudocode)

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

like image 991
TorbenJ Avatar asked Dec 05 '25 18:12

TorbenJ


1 Answers

Create XAML ResourceDictionary files, include them like this:

<Window.Resources>
    <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
              <ResourceDictionary Source="./Styles/TextBlockStyle.xaml" />
         </ResourceDictionary.MergedDictionaries>
         <!-- other resources here -->
    </ResourceDictionary>
</Window.Resources>
like image 122
H.B. Avatar answered Dec 08 '25 12:12

H.B.