Search code examples
c#wpftreeview

Can't change background color of treeview use Mahapp


I try to change treeview's color but the color can't fill all of treeview

It's always like this:

enter image description here

My .xaml

<Grid >
    <TreeView x:Name="treeView"
              BorderThickness="0"
              Style="{x:Null}"
              Height="Auto" Width="Auto"
              Background="#38364E"
              Foreground="#38364E"
              Margin="15,15,15,15">

    </TreeView>
</Grid>

My App.xaml

<Application x:Class="Rodemeyer.Dot2Wpf.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" DispatcherUnhandledException="Application_DispatcherUnhandledException" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml">
        </ResourceDictionary>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml">
        </ResourceDictionary>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml">
        </ResourceDictionary>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml">
        </ResourceDictionary>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml">
        </ResourceDictionary>
        <ResourceDictionary Source="ResourcesDictionary/ButtonStyle.xaml">
          <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:Rodemeyer.Dot2Wpf.ViewModel" />
        </ResourceDictionary>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
</Application>

I guess treeview use color style of mahapp and it can't change background color by manual.

Is there anyone know how to fill color to treeview when use mahapp framework.


Solution

  • You change the colours by overriding some resources that are used in the template, e.g.:

    <TreeView x:Name="treeView"
              BorderThickness="0"
              Height="Auto" Width="Auto"
              Margin="15,15,15,15">
        <TreeView.Resources>
            <SolidColorBrush x:Key="MahApps.Brushes.ThemeBackground" Color="#38364E" />
            <SolidColorBrush x:Key="MahApps.Brushes.Text" Color="Red" />
            <!-- mouse over: -->
            <SolidColorBrush x:Key="MahApps.Brushes.Accent3" Color="Green" />
            <!-- selection: -->
            <SolidColorBrush x:Key="MahApps.Brushes.Accent" Color="Orange" />
        </TreeView.Resources>
    </TreeView>