Skip to content

Instantly share code, notes, and snippets.

@jammerxd
Created March 9, 2016 08:19
Show Gist options
  • Save jammerxd/3d54bac34259320e4531 to your computer and use it in GitHub Desktop.
Save jammerxd/3d54bac34259320e4531 to your computer and use it in GitHub Desktop.
Modified Behaviour
<ResourceDictionary 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"
xmlns:Behaviours="clr-namespace:MahApps.Metro.Behaviours"
xmlns:Converters="clr-namespace:MahApps.Metro.Converters">
<Converters:ThicknessBindingConverter x:Key="ThicknessBindingConverter" />
<Style x:Key="ExpanderBaseHeaderStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Margin" Value="4" />
<Setter Property="Padding" Value="0" />
<Setter Property="SnapsToDevicePixels" Value="True" />
</Style>
<Style x:Key="ExpanderRightHeaderStyle"
BasedOn="{StaticResource ExpanderBaseHeaderStyle}"
TargetType="{x:Type ToggleButton}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<Grid Background="Transparent" SnapsToDevicePixels="False">
<Grid.RowDefinitions>
<RowDefinition Height="19" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid>
<Grid.LayoutTransform>
<TransformGroup>
<TransformGroup.Children>
<TransformCollection>
<RotateTransform Angle="-90" />
</TransformCollection>
</TransformGroup.Children>
</TransformGroup>
</Grid.LayoutTransform>
<Ellipse x:Name="Circle"
Width="19"
Height="19"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Stroke="{TemplateBinding Foreground}" />
<Path x:Name="Arrow"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Stroke="{TemplateBinding Foreground}"
StrokeThickness="2"
Data="M 1,1.5 L 4.5,5 L 8,1.5"
SnapsToDevicePixels="false" />
</Grid>
<Controls:ContentControlEx Grid.Row="1"
Margin="0 4 0 0"
Padding="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="Arrow" Property="Data" Value="M 1,4.5 L 4.5,1 L 8,4.5" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Arrow" Property="Stroke" Value="{DynamicResource GrayBrush2}" />
<Setter TargetName="Circle" Property="Stroke" Value="{DynamicResource GrayBrush2}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Arrow" Property="Stroke" Value="{DynamicResource BlackColorBrush}" />
<Setter TargetName="Circle" Property="Stroke" Value="{DynamicResource BlackColorBrush}" />
<Setter TargetName="Circle" Property="StrokeThickness" Value="2" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ExpanderUpHeaderStyle"
BasedOn="{StaticResource ExpanderBaseHeaderStyle}"
TargetType="{x:Type ToggleButton}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid Background="Transparent" SnapsToDevicePixels="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="19" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid>
<Grid.LayoutTransform>
<TransformGroup>
<TransformGroup.Children>
<TransformCollection>
<RotateTransform Angle="180" />
</TransformCollection>
</TransformGroup.Children>
</TransformGroup>
</Grid.LayoutTransform>
<Ellipse x:Name="Circle"
Width="19"
Height="19"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Stroke="{TemplateBinding Foreground}" />
<Path x:Name="Arrow"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Stroke="{TemplateBinding Foreground}"
StrokeThickness="2"
Data="M 1,1.5 L 4.5,5 L 8,1.5"
SnapsToDevicePixels="false" />
</Grid>
<Controls:ContentControlEx Grid.Column="1"
Margin="4 0 0 0"
Padding="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="Arrow" Property="Data" Value="M 1,4.5 L 4.5,1 L 8,4.5" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Arrow" Property="Stroke" Value="{DynamicResource GrayBrush2}" />
<Setter TargetName="Circle" Property="Stroke" Value="{DynamicResource GrayBrush2}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Arrow" Property="Stroke" Value="{DynamicResource BlackColorBrush}" />
<Setter TargetName="Circle" Property="Stroke" Value="{DynamicResource BlackColorBrush}" />
<Setter TargetName="Circle" Property="StrokeThickness" Value="2" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ExpanderLeftHeaderStyle"
BasedOn="{StaticResource ExpanderRightHeaderStyle}"
TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid Background="Transparent" SnapsToDevicePixels="False">
<Grid.RowDefinitions>
<RowDefinition Height="19" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid>
<Grid.LayoutTransform>
<TransformGroup>
<TransformGroup.Children>
<TransformCollection>
<RotateTransform Angle="90" />
</TransformCollection>
</TransformGroup.Children>
</TransformGroup>
</Grid.LayoutTransform>
<Ellipse x:Name="Circle"
Width="19"
Height="19"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Stroke="{TemplateBinding Foreground}" />
<Path x:Name="Arrow"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Stroke="{TemplateBinding Foreground}"
StrokeThickness="2"
Data="M 1,1.5 L 4.5,5 L 8,1.5"
SnapsToDevicePixels="false" />
</Grid>
<Controls:ContentControlEx Grid.Row="1"
Margin="0 4 0 0"
Padding="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="Arrow" Property="Data" Value="M 1,4.5 L 4.5,1 L 8,4.5" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Arrow" Property="Stroke" Value="{DynamicResource GrayBrush2}" />
<Setter TargetName="Circle" Property="Stroke" Value="{DynamicResource GrayBrush2}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Arrow" Property="Stroke" Value="{DynamicResource BlackColorBrush}" />
<Setter TargetName="Circle" Property="Stroke" Value="{DynamicResource BlackColorBrush}" />
<Setter TargetName="Circle" Property="StrokeThickness" Value="2" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ExpanderDownHeaderStyle"
BasedOn="{StaticResource ExpanderUpHeaderStyle}"
TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid Background="Transparent" SnapsToDevicePixels="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="19" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Ellipse x:Name="Circle"
Width="19"
Height="19"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Stroke="{TemplateBinding Foreground}" />
<Path x:Name="Arrow"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Stroke="{TemplateBinding Foreground}"
StrokeThickness="2"
Data="M 1,1.5 L 4.5,5 L 8,1.5"
SnapsToDevicePixels="false" />
<Controls:ContentControlEx Grid.Column="1"
Margin="4 0 0 0"
Padding="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="Arrow" Property="Data" Value="M 1,4.5 L 4.5,1 L 8,4.5" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Arrow" Property="Stroke" Value="{DynamicResource GrayBrush2}" />
<Setter TargetName="Circle" Property="Stroke" Value="{DynamicResource GrayBrush2}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Arrow" Property="Stroke" Value="{DynamicResource BlackColorBrush}" />
<Setter TargetName="Circle" Property="Stroke" Value="{DynamicResource BlackColorBrush}" />
<Setter TargetName="Circle" Property="StrokeThickness" Value="2" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="MetroExpander" TargetType="{x:Type Expander}">
<Setter Property="Foreground" Value="{DynamicResource BlackBrush}" />
<Setter Property="Background" Value="{DynamicResource AccentColorBrush}" />
<Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}" />
<Setter Property="Controls:ExpanderHelper.Transition" Value="Left"/>
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Upper" />
<Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="{DynamicResource ContentFontSize}" />
<Setter Property="Controls:ExpanderHelper.HeaderDownStyle" Value="{StaticResource ExpanderDownHeaderStyle}" />
<Setter Property="Controls:ExpanderHelper.HeaderLeftStyle" Value="{StaticResource ExpanderLeftHeaderStyle}" />
<Setter Property="Controls:ExpanderHelper.HeaderRightStyle" Value="{StaticResource ExpanderRightHeaderStyle}" />
<Setter Property="Controls:ExpanderHelper.HeaderUpStyle" Value="{StaticResource ExpanderUpHeaderStyle}" />
<Setter Property="Controls:GroupBoxHelper.HeaderForeground" Value="{x:Null}" />
<Setter Property="Padding" Value="5" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Expander}">
<Grid>
<Rectangle x:Name="DisabledVisualElement"
Fill="{DynamicResource ControlsDisabledBrush}"
Opacity="0"
IsHitTestVisible="false" />
<DockPanel x:Name="ExpanderRoot">
<Border x:Name="HeaderSite"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
DockPanel.Dock="Top"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
UseLayoutRounding="True">
<ToggleButton x:Name="ToggleSite"
Style="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ExpanderHelper.HeaderDownStyle)}"
FontSize="{TemplateBinding Controls:ControlsHelper.HeaderFontSize}"
FontWeight="{TemplateBinding Controls:ControlsHelper.HeaderFontWeight}"
FontStretch="{TemplateBinding Controls:ControlsHelper.HeaderFontStretch}"
Controls:ControlsHelper.ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
Content="{TemplateBinding Header}"
ContentStringFormat="{TemplateBinding HeaderStringFormat}"
ContentTemplate="{TemplateBinding HeaderTemplate}"
ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
UseLayoutRounding="False">
<ToggleButton.Foreground>
<MultiBinding Converter="{x:Static Converters:BackgroundToForegroundConverter.Instance}">
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="Background"
Mode="OneWay" />
<Binding RelativeSource="{RelativeSource TemplatedParent}"
Path="(Controls:GroupBoxHelper.HeaderForeground)"
Mode="OneWay" />
</MultiBinding>
</ToggleButton.Foreground>
</ToggleButton>
</Border>
<Border x:Name="ExpandSite"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Background="Transparent"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="0"
Opacity="1"
DockPanel.Dock="Bottom"
Focusable="false"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
UseLayoutRounding="True"
Visibility="Collapsed">
<Controls:TransitioningContentControl
Behaviours:ReloadBehavior.OnExpanderExpandedChanged="True"
RestartTransitionOnContentChange="True"
Transition="{TemplateBinding Controls:ExpanderHelper.Transition}">
<ContentPresenter Margin="{TemplateBinding Padding}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Cursor="{TemplateBinding Cursor}"
UseLayoutRounding="False" />
</Controls:TransitioningContentControl>
</Border>
</DockPanel>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity">
<SplineDoubleKeyFrame KeyTime="0" Value="0.7" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ExpanderRoot" Storyboard.TargetProperty="(UIElement.Opacity)">
<EasingDoubleKeyFrame KeyTime="0" Value="0.3" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="true">
<Setter TargetName="ExpandSite" Property="Visibility" Value="Visible" />
<!--<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.25"
Storyboard.TargetName="ExpandSite"
Storyboard.TargetProperty="(UIElement.Opacity)"
To="1" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.25"
Storyboard.TargetName="ExpandSite"
Storyboard.TargetProperty="(UIElement.Opacity)"
To="0" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>-->
</Trigger>
<Trigger Property="ExpandDirection" Value="Right">
<Setter TargetName="ExpandSite" Property="BorderThickness" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessBindingConverter}, ConverterParameter={x:Static Converters:IgnoreThicknessSideType.Left}}" />
<Setter TargetName="ExpandSite" Property="DockPanel.Dock" Value="Right" />
<Setter TargetName="HeaderSite" Property="DockPanel.Dock" Value="Left" />
<Setter TargetName="ToggleSite" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ExpanderHelper.HeaderRightStyle)}" />
</Trigger>
<Trigger Property="ExpandDirection" Value="Up">
<Setter TargetName="ExpandSite" Property="BorderThickness" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessBindingConverter}, ConverterParameter={x:Static Converters:IgnoreThicknessSideType.Bottom}}" />
<Setter TargetName="ExpandSite" Property="DockPanel.Dock" Value="Top" />
<Setter TargetName="HeaderSite" Property="DockPanel.Dock" Value="Bottom" />
<Setter TargetName="ToggleSite" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ExpanderHelper.HeaderUpStyle)}" />
</Trigger>
<Trigger Property="ExpandDirection" Value="Left">
<Setter TargetName="ExpandSite" Property="BorderThickness" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessBindingConverter}, ConverterParameter={x:Static Converters:IgnoreThicknessSideType.Right}}" />
<Setter TargetName="ExpandSite" Property="DockPanel.Dock" Value="Left" />
<Setter TargetName="HeaderSite" Property="DockPanel.Dock" Value="Right" />
<Setter TargetName="ToggleSite" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ExpanderHelper.HeaderLeftStyle)}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
using System.Windows;
using System.Windows.Controls;
namespace MahApps.Metro.Controls
{
using System.ComponentModel;
/// <summary>
/// A helper class that provides various attached properties for the Expander control.
/// <see cref="Expander"/>
/// </summary>
public static class ExpanderHelper
{
public static readonly DependencyProperty HeaderUpStyleProperty = DependencyProperty.RegisterAttached("HeaderUpStyle", typeof(Style), typeof(ExpanderHelper), new FrameworkPropertyMetadata((Style)null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
/// <summary>
/// Gets the toggle button style used for the ExpandDirection Up.
/// </summary>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(Expander))]
public static Style GetHeaderUpStyle(UIElement element)
{
return (Style)element.GetValue(HeaderUpStyleProperty);
}
/// <summary>
/// Sets the toggle button style used for the ExpandDirection Up.
/// </summary>
public static void SetHeaderUpStyle(UIElement element, Style value)
{
element.SetValue(HeaderUpStyleProperty, value);
}
public static readonly DependencyProperty HeaderDownStyleProperty = DependencyProperty.RegisterAttached("HeaderDownStyle", typeof(Style), typeof(ExpanderHelper), new FrameworkPropertyMetadata((Style)null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
/// <summary>
/// Gets the toggle button style used for the ExpandDirection Down.
/// </summary>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(Expander))]
public static Style GetHeaderDownStyle(UIElement element)
{
return (Style)element.GetValue(HeaderDownStyleProperty);
}
/// <summary>
/// Sets the toggle button style used for the ExpandDirection Down.
/// </summary>
public static void SetHeaderDownStyle(UIElement element, Style value)
{
element.SetValue(HeaderDownStyleProperty, value);
}
public static readonly DependencyProperty HeaderLeftStyleProperty = DependencyProperty.RegisterAttached("HeaderLeftStyle", typeof(Style), typeof(ExpanderHelper), new FrameworkPropertyMetadata((Style)null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
/// <summary>
/// Gets the toggle button style used for the ExpandDirection Left.
/// </summary>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(Expander))]
public static Style GetHeaderLeftStyle(UIElement element)
{
return (Style)element.GetValue(HeaderLeftStyleProperty);
}
/// <summary>
/// Sets the toggle button style used for the ExpandDirection Left.
/// </summary>
public static void SetHeaderLeftStyle(UIElement element, Style value)
{
element.SetValue(HeaderLeftStyleProperty, value);
}
public static readonly DependencyProperty HeaderRightStyleProperty = DependencyProperty.RegisterAttached("HeaderRightStyle", typeof(Style), typeof(ExpanderHelper), new FrameworkPropertyMetadata((Style)null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
/// <summary>
/// Gets the toggle button style used for the ExpandDirection Right.
/// </summary>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(Expander))]
public static Style GetHeaderRightStyle(UIElement element)
{
return (Style)element.GetValue(HeaderRightStyleProperty);
}
/// <summary>
/// Sets the toggle button style used for the ExpandDirection Right.
/// </summary>
public static void SetHeaderRightStyle(UIElement element, Style value)
{
element.SetValue(HeaderRightStyleProperty, value);
}
public static readonly DependencyProperty TransitionProperty =
DependencyProperty.RegisterAttached("Transition", typeof(TransitionType), typeof(ExpanderHelper),
new FrameworkPropertyMetadata(TransitionType.Default, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.Inherits));
[Category(AppName.MahApps)]
public static TransitionType GetTransition(DependencyObject obj)
{
return (TransitionType)obj.GetValue(TransitionProperty);
}
public static void SetTransition(DependencyObject obj, TransitionType value)
{
obj.SetValue(TransitionProperty, value);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MahApps.Metro.Controls;
namespace MahApps.Metro.Behaviours
{
using System.ComponentModel;
public static class ReloadBehavior
{
public static DependencyProperty OnDataContextChangedProperty =
DependencyProperty.RegisterAttached("OnDataContextChanged", typeof(bool), typeof(ReloadBehavior), new PropertyMetadata(OnDataContextChanged));
[Category(AppName.MahApps)]
public static bool GetOnDataContextChanged(MetroContentControl element)
{
return (bool)element.GetValue(OnDataContextChangedProperty);
}
public static void SetOnDataContextChanged(MetroContentControl element, bool value)
{
element.SetValue(OnDataContextChangedProperty, value);
}
private static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MetroContentControl)d).DataContextChanged += ReloadDataContextChanged;
}
static void ReloadDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
((MetroContentControl)sender).Reload();
}
public static DependencyProperty OnSelectedTabChangedProperty =
DependencyProperty.RegisterAttached("OnSelectedTabChanged", typeof(bool), typeof(ReloadBehavior), new PropertyMetadata(OnSelectedTabChanged));
[Category(AppName.MahApps)]
public static bool GetOnSelectedTabChanged(ContentControl element)
{
return (bool)element.GetValue(OnDataContextChangedProperty);
}
public static void SetOnSelectedTabChanged(ContentControl element, bool value)
{
element.SetValue(OnDataContextChangedProperty, value);
}
private static void OnSelectedTabChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ContentControl)d).Loaded += ReloadLoaded;
}
static void ReloadLoaded(object sender, RoutedEventArgs e)
{
var metroContentControl = ((ContentControl)sender);
var tab = Ancestors(metroContentControl)
.OfType<TabControl>()
.FirstOrDefault();
if (tab == null) return;
SetMetroContentControl(tab, metroContentControl);
tab.SelectionChanged -= ReloadSelectionChanged;
tab.SelectionChanged += ReloadSelectionChanged;
}
private static IEnumerable<DependencyObject> Ancestors(DependencyObject obj)
{
var parent = VisualTreeHelper.GetParent(obj);
while (parent != null)
{
yield return parent;
obj = parent;
parent = VisualTreeHelper.GetParent(obj);
}
}
static void ReloadSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.OriginalSource != sender)
return;
var contentControl = GetMetroContentControl((TabControl)sender);
var metroContentControl = contentControl as MetroContentControl;
if (metroContentControl != null)
{
metroContentControl.Reload();
}
var transitioningContentControl = contentControl as TransitioningContentControl;
if (transitioningContentControl != null)
{
transitioningContentControl.ReloadTransition();
}
}
public static readonly DependencyProperty MetroContentControlProperty =
DependencyProperty.RegisterAttached("MetroContentControl", typeof(ContentControl), typeof(ReloadBehavior), new PropertyMetadata(default(ContentControl)));
public static void SetMetroContentControl(UIElement element, ContentControl value)
{
element.SetValue(MetroContentControlProperty, value);
}
[Category(AppName.MahApps)]
public static ContentControl GetMetroContentControl(UIElement element)
{
return (ContentControl)element.GetValue(MetroContentControlProperty);
}
#region New For Expander
public static DependencyProperty OnExpanderExpandedChangedProperty = DependencyProperty.RegisterAttached("OnExpanderExpandedChanged", typeof(bool), typeof(ReloadBehavior), new PropertyMetadata(OnExpanderExpandedChanged));
[Category(AppName.MahApps)]
public static bool GetOnExpanderExpandedChanged(ContentControl element)
{
return (bool)element.GetValue(OnExpanderExpandedChangedProperty);
}
public static void SetOnExpanderExpandedChanged(ContentControl element, bool value)
{
element.SetValue(OnExpanderExpandedChangedProperty, value);
}
private static void OnExpanderExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ContentControl)d).Loaded += ReloadLoadedExpander;
}
static void ReloadLoadedExpander(object sender, RoutedEventArgs e)
{
var metroContentControl = ((ContentControl)sender);
var tab = Ancestors(metroContentControl)
.OfType<Expander>()
.FirstOrDefault();
if (tab == null) return;
SetMetroContentControl(tab, metroContentControl);
tab.Expanded -= ReloadSelectionChangedExpander;
tab.Expanded += ReloadSelectionChangedExpander;
}
static void ReloadSelectionChangedExpander(object sender, RoutedEventArgs e)
{
if (e.OriginalSource != sender)
return;
var contentControl = GetMetroContentControl((Expander)sender);
var metroContentControl = contentControl as MetroContentControl;
if (metroContentControl != null)
{
metroContentControl.Reload();
}
var transitioningContentControl = contentControl as TransitioningContentControl;
if (transitioningContentControl != null)
{
transitioningContentControl.ReloadTransition();
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment