【问题标题】:Mahapps metro wpf style doesn't work in class libraryMahapps Metro wpf 样式在类库中不起作用
【发布时间】:2015-08-17 11:50:53
【问题描述】:

我正在研究使用 MVVM 模式的解决方案。主应用程序有一个窗口,该窗口继承了其他用户控件以及类库中的一些功能。例如,在用户控件中,我有一个带有上下文菜单的树视图,树视图不继承 mahapps Metro 风格,但上下文菜单可以。我需要做什么?

这是我的结构示例:

主应用程序:

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

MainView.xaml 代码:

        <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 代码:

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

类库:

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

UserControlView.xaml 代码:

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

【问题讨论】:

    标签: c# wpf mahapps.metro


    【解决方案1】:

    我发现了问题...我在 UserControlView.xaml 中定义了用户控件资源,现在运行良好

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多