Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load control style from separate file in wpf

Tags:

c#

.net

wpf

I have the following style added to my Windows.Resources

<Window.Resources>
...
<!--A Style that extends the previous TextBlock Style-->
<!--This is a "named style" with an x:Key of TitleText-->
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
   TargetType="TextBlock"
   x:Key="TitleText">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Foreground">
 <Setter.Value>
  <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
    <LinearGradientBrush.GradientStops>
      <GradientStop Offset="0.0" Color="#90DDDD" />
      <GradientStop Offset="1.0" Color="#5BFFFF" />
    </LinearGradientBrush.GradientStops>
  </LinearGradientBrush>
  </Setter.Value>
</Setter>
</Style> 
...
</Window.Resources>

I have a lot of those styles in my xaml code and I would like to save each component style to an extra file (not an external file).. for example all the styles related to TextBlocks should be in a file called TextBlockStyles.xaml

How would I do this in wpf?

How do I link the style in my project ?

Thanks in advance

like image 420
lebhero Avatar asked Jun 11 '12 10:06

lebhero


3 Answers

You use merged resource dictionaries

In you app.xaml you would use

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
                Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
            ... other dictionaries here
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

or directly into a UserControl would be

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
                Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
            ... other dictionaries here
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

You can shorten Source="..." to just Source="TextBlockStyles.xaml" if the file is in the same assembly and in the root of the project, or alternatively Source="Styles\TextBlockStyles.xaml" if you put the resource dictionary into the folder Styles.

like image 127
Phil Avatar answered Oct 22 '22 12:10

Phil


Use case: you have a user control called MyView.xaml with a button. You want to style the button with an external XAML file.


In MyView.xaml:

<User Control ...namespaces...>
    <UserControl.Resources>
        <ResourceDictionary>
            ...converters...

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MyButton.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

     ...the rest of the control...
</UserControl>

In MyButton.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MSDNSample">

    <Style x:Key="FooButton" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Pink" />
    </Style>
</ResourceDictionary>

Back to MyView.xaml ("the rest of the control"):

<Button Style="{StaticResource FooButton}">
    Hello World
</Button>
like image 44
Ben Avatar answered Oct 22 '22 12:10

Ben


In Solution Explorer Right Click on your Project Select Add After that click on Resource Dictionary... Choose name and add to your project. Open App.xaml Add This Code in Application Tag

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="YourStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

In YourStyle.xaml :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:APPNAME">
    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Pink" />
    </Style>
</ResourceDictionary>
like image 27
MdRezaV Avatar answered Oct 22 '22 13:10

MdRezaV