我在使用MVVM模式的解决方案上工作。主应用程序有一个窗口,它继承了其他用户控件和类库中的一些函数。例如,在用户控件中,我有一个具有上下文菜单的树视图,树视图不继承mahapps metro风格,但是上下文菜单。我需要做什么?
这是我的结构示例:
MainApp:
->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>
ClassLibrary:
->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>
答案 0 :(得分:2)
我发现了问题...我在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>