2015-11-05 29 views
0

我有一个MetroWindow,它使用默认颜色加载。我有一个选项可供用户更改样式。在窗口中,我使用Content控件从初始化此窗口时的代码后面动态设置用户控件(此窗口从其他应用程序初始化,因此没有app.xaml,我使用窗口本身的资源)。主窗口的代码如下。在用户控件中,我使用了一些标签和一个选项卡控件。在窗口中,我有一个选项让用户能够从可用主题中选择主题(如在演示项目中)。在所有标签中,我使用动态资源。Mahapps,不适用于用户控件的主题

第一次加载窗口时,将应用默认颜色,并且所有内部控件也具有相同的主题。当我从窗口顶部更改主题时,窗口边框,标题,辉光发生变化,但内部控件保留旧主题(如选项卡控制标题等)。我尝试了所有内容,但似乎并未发生变化。

下面是我的代码:

<Controls:MetroWindow x:Class="Test.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
      Title="MainWindow" Height="300" Width="300" ShowMaxRestoreButton="False" ShowMinButton="False" 
      BorderBrush="{DynamicResource HighlightBrush}" 
      GlowBrush="{DynamicResource HighlightBrush}" WindowStartupLocation="CenterScreen" 
      ResizeMode="NoResize" WindowTransitionsEnabled="False" TitleCaps="True" 
      BorderThickness="1"> 
    <Window.Resources> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <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/BaseLight.xaml" /> 
     <ResourceDictionary Source="/Test;component/Resources/Icons.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 

     <Border Margin="5" BorderBrush="Transparent" BorderThickness="0"> 
      <ContentControl x:Name="ChildContentControl" 
         HorizontalAlignment="Stretch" 
         VerticalAlignment="Stretch"> 
      </ContentControl> 
     </Border> 
    </Controls:MetroWindow> 

我的代码背后的窗口:

public partial class MainWindow : MetroWindow 
{ 

    public MainWindow() 
    { 
     InitializeComponent(); 
     this.Initialize(); 
    } 

    public MainWindow(string title, UserControl childControl) 
    { 
     try 
     { 
      InitializeComponent(); 
      this.Title = title; 
      this.ChildContentControl.Content = childControl; 
     } 
     catch (Exception de) 
     { 
      throw de; 
     } 
    } 
} 

我的用户控制XAML

<UserControl x:Class="Test.UCDemo" 
      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:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:sys="clr-namespace:System;assembly=mscorlib" 
      xmlns:local="clr-namespace:Test" 
      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="/Test;component/Resources/Icons.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 


    </UserControl.Resources> 
    <Border> 
    <Grid> 
     <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <DockPanel Grid.Row="0" LastChildFill="True"> 

     <Grid Height="60" DockPanel.Dock="Bottom"> 

      <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="auto"/> 
      <ColumnDefinition Width="5"/> 
      </Grid.ColumnDefinitions> 

      <Button Grid.Column="1" Margin="5,0,5,0" Width="50" Height="50" 
       Style="{DynamicResource MetroCircleButtonStyle}" Command="{Binding OKCommand}"> 
      <Rectangle Width="20" 
       Height="20" 
       Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"> 
       <Rectangle.OpacityMask> 
       <VisualBrush Stretch="Fill" 
        Visual="{StaticResource appbar_check}" /> 
       </Rectangle.OpacityMask> 
      </Rectangle> 
      </Button> 
     </Grid> 
     <Grid DockPanel.Dock="Top" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent" 
        SnapsToDevicePixels="True" 
        ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"> 

      <Grid.RowDefinitions> 
      <RowDefinition Height="*"></RowDefinition> 
      <RowDefinition Height="auto"></RowDefinition> 
      </Grid.RowDefinitions> 

      <Grid Grid.Row="0"> 
      <Grid.Resources> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.xaml" /> 
      </Grid.Resources> 
      <TabControl Margin="0,10,0,0" TabStripPlacement="Left" ItemsSource="{Binding Catalogs}" SelectedItem="{Binding SelectedCatalog}"> 
       <TabControl.ItemTemplate> 
       <!-- this is the header template--> 
       <DataTemplate> 
        <TextBlock Text="{Binding Path=CaptionCat}" Foreground="{DynamicResource HighlightBrush}" FontSize="14" Margin="0,0,0,10" /> 
       </DataTemplate> 
       </TabControl.ItemTemplate> 
       <TabControl.ContentTemplate> 
       <DataTemplate> 
        <ItemsControl ItemsSource="{Binding DataContext.SelectedCatalog.Books,RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}"> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
         <WrapPanel ScrollViewer.VerticalScrollBarVisibility="Disabled"/> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
         <Controls:Tile Height="125" Width="300" IsEnabled="{Binding IsEnabled}" Title="{Binding CaptionBook}"/> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
        </ItemsControl> 
       </DataTemplate> 
       </TabControl.ContentTemplate> 
      </TabControl> 
      </Grid> 
     </Grid> 

     </DockPanel> 
    </Grid> 
    </Border> 


</UserControl> 

我的用户控件的代码背后:

public partial class UCDemo : UserControl 
{ 

    private VMDemo CatalogVM { set; get; } 

    public UCDemo() 
    { 
     try 
     { 
      InitializeComponent(); 
      this.DataContext = new VMDemo(); 
     } 
     catch (Exception de) 
     { 
      throw de; 
     } 
    } 
} 

终于在这里就是我所说的窗口,从托管的应用程序代码嵌入的用户控件:

private void ShowDemoUI() 
    { 
     try 
     { 
      var child = new UILibrary.UCDemo(); 
      UILibrary.MainWindow window = new UILibrary.MainWindow("Book catalog", child); 
      window.Height = 450; 
      window.Width = 700; 
      window.ShowDialog(); 
     } 
     catch (Exception de) 
     { 
      throw de; 
     } 
    } 
  • 吉里贾·
+0

你能告诉我们你的UserControl和代码吗? – David

+0

@David:我修改了主帖并添加了所有相关代码 - Shankar – Shankar

回答

0

我想你有2个选择来解决这个问题。

  • VMDemo类型创建Test.UCDemo为DataTemplate中,并通过VMDemo此模板的Content,所以你ContentControl会自动为你做魔术(它只是只是一个建议,我不测试它)
  • 将资源也放入您的Test.UCDemo

希望有帮助。

+0

我正在使用代码。所以我选择了第二种方法,这是有效的。谢谢:) – Shankar

+0

@Shankar你做了什么来解决这个问题?我有同样的问题。我尝试将所有MahApps资源添加到我的用户控件中,但当我更改口音时(通过ThemeManager),颜色仍然不会更新。 –

0

你需要将所有ResourceDictionaries移动在App.xaml

之后,我认为你需要看看Theme Manager

这是我所做的。在这个例子中我所有的Content Control都保存Theme即使我改变主题,点击一个按钮:

public ShellView() 
{ 
    this.InitializeComponent(); 
    var theme = ThemeManager.DetectAppStyle(Application.Current); 
    ThemeManager.ChangeAppStyle(Application.Current, ThemeManager.GetAccent("Blue"), theme.Item1); 
} 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var theme = ThemeManager.DetectAppStyle(Application.Current); 
    ThemeManager.ChangeAppStyle(Application.Current, ThemeManager.GetAccent("Cyan"), theme.Item1); 
} 

的代码需要在代码behing你的窗口(在我的情况下,它ShellView.cs)为你可以假设它是MainWindow.cs

这是Custom Theme的另一个例子。

public ShellView() 
{ 
    this.InitializeComponent(); 
    ThemeManager.AddAccent("YourAccent", new Uri("pack://application:,,,/YourAccent.xaml")); 

    // Set your Custom Theme when the Window start 
    var theme = ThemeManager.DetectAppStyle(Application.Current); 
    ThemeManager.ChangeAppStyle(Application.Current, ThemeManager.GetAccent("YourAccent"), theme.Item1); 
} 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    // Change the Theme to Cyan on a simple button click 
    var theme = ThemeManager.DetectAppStyle(Application.Current); 
    ThemeManager.ChangeAppStyle(Application.Current, ThemeManager.GetAccent("Cyan"), theme.Item1); 
} 
+0

我没有app.xaml。这是在我原来的帖子中提到的。我的WPF项目是WPF类库,而不是WPF应用程序。我从另一个应用程序调用它。所以我没有App.xaml。我实际上没有应用程序对象(Application.Current为null),所以对于chaning主题,我使用“ThemeManager.ChangeAppStyle(this,ThemeManager.GetAccent(”Cyan“),theme.Item1);”这是窗口对象。我甚至尝试初始化应用程序对象(通过新的应用程序())并通过代码添加资源,但这也不起作用。 – Shankar

+0

顺便说一句,我使用ThemeManager来改变主题,上面给出的确切代码。但它只会改变Window主题,而不会改变内部的ContentControl嵌入式用户控件。 – Shankar