kaan_93
kaan_93

Reputation: 85

Mahapps metro wpf style doesn't work in class library

I work on solution where I use the MVVM patern. The main app has a window which inherits other user controls with some functions from class libraries. For example in a user control I have a treeview which has a context menu, the treeview doesn't inherit the mahapps metro style but the context menu does. What I need to do?

Here is my structure example:

MainApp:

->Model
->View   ->MainView.xaml
->ViewModel
->App.xaml

MainView.xaml code:

        <Controls:MetroWindow x:Class="MainApp.View.MainView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                xmlns:uc="clr-namespace:ClassLibrary.View;assembly=ClassLibrary"
                xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"            
                Title="MainView" Height="500" Width="800" MinWidth="600" MinHeight="400"

                EnableDWMDropShadow="True"
                ResizeMode="CanResizeWithGrip"

                WindowTransitionsEnabled="False"
                WindowStartupLocation="CenterScreen">

            <Grid>
                <Grid Margin="184,0,0,0">
                    <uc:UserControlView/>
                </Grid>
            </Grid>

        </Controls:MetroWindow>

App.xaml code:

    <Application x:Class="MainApp.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Views/LoginWindow.xaml">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Resources/Icons.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />

                    <!-- accent resource -->
                    <!-- change "Cobalt" to the accent color you want -->

                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Red.xaml" />

                    <!-- theme resource -->
                    <!-- change "BaseLight" to the theme you want -->
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>

ClassLibrary:

->Model
->View
->UserControlView.xaml
->ViewModel

UserControlView.xaml code:

<UserControl x:Class="ClassLibrary.View.UserControlView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"

             mc:Ignorable="d" 
             d:DesignHeight="350" d:DesignWidth="525">
    <Grid>
        <Label x:Name="label_selected" HorizontalAlignment="Left" Margin="252,10,0,0" VerticalAlignment="Top" Width="85" Height="26" Content="Selected Item: "/>
        <TreeView Margin="0,0,286,10">
            <TreeViewItem Header="Hello">
                <TreeViewItem Header="Hello"></TreeViewItem>
                <TreeViewItem Header="Hello"></TreeViewItem>
            </TreeViewItem>
            <TreeViewItem Header="Hello"></TreeViewItem>
            <TreeViewItem Header="Hello"></TreeViewItem>
            <TreeViewItem Header="Hello"></TreeViewItem>
        </TreeView>
        <Label x:Name="label_selectedItem" HorizontalAlignment="Left" Margin="337,10,0,0" VerticalAlignment="Top" Width="170" Height="26" Content="{Binding CurrentItem.Item, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>

    </Grid>
</UserControl>

Upvotes: 2

Views: 2052

Answers (1)

kaan_93
kaan_93

Reputation: 85

I found the issue...I defined the user control resources in the UserControlView.xaml and now is working very well

<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Resources/Icons.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />

                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

Upvotes: 3

Related Questions