Skip to content

Commit 0f0372d

Browse files
committed
here we go with drawer control. think gonna have to move the slide animation into code.
1 parent 0ba668e commit 0f0372d

File tree

6 files changed

+401
-104
lines changed

6 files changed

+401
-104
lines changed

MainDemo.Wpf/MainWindow.xaml

Lines changed: 110 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -34,107 +34,115 @@
3434
</Window.Resources>
3535

3636
<wpf:DialogHost Identifier="RootDialog">
37-
<DockPanel>
38-
<wpf:ColorZone Padding="16" wpf:ShadowAssist.ShadowDepth="Depth2"
39-
Mode="PrimaryMid" DockPanel.Dock="Top">
40-
<DockPanel>
41-
<ToggleButton Style="{StaticResource MaterialDesignHamburgerToggleButton}" IsChecked="True"
42-
x:Name="MenuToggleButton"/>
43-
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22">Material Design In XAML Toolkit</TextBlock>
44-
</DockPanel>
45-
</wpf:ColorZone>
46-
<ListBox DockPanel.Dock="Left" x:Name="DemoItemsListBox" Margin="16 16 0 16" SelectedIndex="0"
47-
Background="{DynamicResource PrimaryHueMidBrush}"
48-
Foreground="{DynamicResource PrimaryHueMidForegroundBrush}"
49-
Visibility="{Binding ElementName=MenuToggleButton, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}">
50-
<ListBox.ItemTemplate>
51-
<DataTemplate DataType="domain:DemoItem">
52-
<TextBlock Text="{Binding Name}" />
53-
</DataTemplate>
54-
</ListBox.ItemTemplate>
55-
<domain:DemoItem Name="Home">
56-
<domain:DemoItem.Content>
57-
<wpfExample:Home />
58-
</domain:DemoItem.Content>
59-
</domain:DemoItem>
60-
<domain:DemoItem Name="Palette">
61-
<domain:DemoItem.Content>
62-
<wpfExample:PaletteSelector>
63-
<wpfExample:PaletteSelector.DataContext>
64-
<wpfExample:PaletteSelectorViewModel />
65-
</wpfExample:PaletteSelector.DataContext>
66-
</wpfExample:PaletteSelector>
67-
</domain:DemoItem.Content>
68-
</domain:DemoItem>
69-
<domain:DemoItem Name="Buttons &amp; Toggles">
70-
<domain:DemoItem.Content>
71-
<wpfExample:Buttons />
72-
</domain:DemoItem.Content>
73-
</domain:DemoItem>
74-
<domain:DemoItem Name="Fields">
75-
<domain:DemoItem.Content>
76-
<wpfExample:TextFields />
77-
</domain:DemoItem.Content>
78-
</domain:DemoItem>
79-
<domain:DemoItem Name="Cards">
80-
<domain:DemoItem.Content>
81-
<wpfExample:Cards />
82-
</domain:DemoItem.Content>
83-
</domain:DemoItem>
84-
<domain:DemoItem Name="Colour Zones">
85-
<domain:DemoItem.Content>
86-
<wpfExample:ColorZones />
87-
</domain:DemoItem.Content>
88-
</domain:DemoItem>
89-
<domain:DemoItem Name="Lists">
90-
<domain:DemoItem.Content>
91-
<wpfExample:Lists>
92-
<wpfExample:Lists.DataContext>
93-
<domain:ListsAndGridsViewModel />
94-
</wpfExample:Lists.DataContext>
95-
</wpfExample:Lists>
96-
</domain:DemoItem.Content>
97-
</domain:DemoItem>
98-
<domain:DemoItem Name="Trees">
99-
<domain:DemoItem.Content>
100-
<wpfExample:Trees />
101-
</domain:DemoItem.Content>
102-
</domain:DemoItem>
103-
<domain:DemoItem Name="Grids">
104-
<domain:DemoItem.Content>
105-
<wpfExample:Grids>
106-
<wpfExample:Grids.DataContext>
107-
<domain:ListsAndGridsViewModel />
108-
</wpfExample:Grids.DataContext>
109-
</wpfExample:Grids>
110-
</domain:DemoItem.Content>
111-
</domain:DemoItem>
112-
<domain:DemoItem Name="Menus &amp; Tool Bars">
113-
<domain:DemoItem.Content>
114-
<wpfExample:MenusAndToolBars />
115-
</domain:DemoItem.Content>
116-
</domain:DemoItem>
117-
<domain:DemoItem Name="Progress Indicators">
118-
<domain:DemoItem.Content>
119-
<wpfExample:Progress />
120-
</domain:DemoItem.Content>
121-
</domain:DemoItem>
122-
<domain:DemoItem Name="Dialogs">
123-
<domain:DemoItem.Content>
124-
<wpfExample:Dialogs>
125-
<wpfExample:Dialogs.DataContext>
126-
<domain:DialogsViewModel />
127-
</wpfExample:Dialogs.DataContext>
128-
</wpfExample:Dialogs>
129-
</domain:DemoItem.Content>
130-
</domain:DemoItem>
131-
<domain:DemoItem Name="Shadows">
132-
<domain:DemoItem.Content>
133-
<wpfExample:Shadows />
134-
</domain:DemoItem.Content>
135-
</domain:DemoItem>
136-
</ListBox>
137-
<ContentControl Margin="16" Content="{Binding ElementName=DemoItemsListBox, Path=SelectedItem.Content}" />
138-
</DockPanel>
37+
<wpf:DrawerHost IsLeftDrawerOpen="{Binding ElementName=MenuToggleButton, Path=IsChecked}">
38+
<wpf:DrawerHost.LeftDrawerContent>
39+
<StackPanel Orientation="Vertical">
40+
<ToggleButton Style="{StaticResource MaterialDesignHamburgerToggleButton}"
41+
HorizontalAlignment="Right" Margin="16"
42+
IsChecked="{Binding ElementName=MenuToggleButton, Path=IsChecked, Mode=TwoWay}" />
43+
<ListBox x:Name="DemoItemsListBox" Margin="0 16 0 16" SelectedIndex="0"
44+
PreviewMouseLeftButtonUp="UIElement_OnPreviewMouseLeftButtonUp"
45+
Visibility="{Binding ElementName=MenuToggleButton, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}">
46+
<ListBox.ItemTemplate>
47+
<DataTemplate DataType="domain:DemoItem">
48+
<TextBlock Text="{Binding Name}" Margin="32 0 32 0" />
49+
</DataTemplate>
50+
</ListBox.ItemTemplate>
51+
<domain:DemoItem Name="Home">
52+
<domain:DemoItem.Content>
53+
<wpfExample:Home />
54+
</domain:DemoItem.Content>
55+
</domain:DemoItem>
56+
<domain:DemoItem Name="Palette">
57+
<domain:DemoItem.Content>
58+
<wpfExample:PaletteSelector>
59+
<wpfExample:PaletteSelector.DataContext>
60+
<wpfExample:PaletteSelectorViewModel />
61+
</wpfExample:PaletteSelector.DataContext>
62+
</wpfExample:PaletteSelector>
63+
</domain:DemoItem.Content>
64+
</domain:DemoItem>
65+
<domain:DemoItem Name="Buttons &amp; Toggles">
66+
<domain:DemoItem.Content>
67+
<wpfExample:Buttons />
68+
</domain:DemoItem.Content>
69+
</domain:DemoItem>
70+
<domain:DemoItem Name="Fields">
71+
<domain:DemoItem.Content>
72+
<wpfExample:TextFields />
73+
</domain:DemoItem.Content>
74+
</domain:DemoItem>
75+
<domain:DemoItem Name="Cards">
76+
<domain:DemoItem.Content>
77+
<wpfExample:Cards />
78+
</domain:DemoItem.Content>
79+
</domain:DemoItem>
80+
<domain:DemoItem Name="Colour Zones">
81+
<domain:DemoItem.Content>
82+
<wpfExample:ColorZones />
83+
</domain:DemoItem.Content>
84+
</domain:DemoItem>
85+
<domain:DemoItem Name="Lists">
86+
<domain:DemoItem.Content>
87+
<wpfExample:Lists>
88+
<wpfExample:Lists.DataContext>
89+
<domain:ListsAndGridsViewModel />
90+
</wpfExample:Lists.DataContext>
91+
</wpfExample:Lists>
92+
</domain:DemoItem.Content>
93+
</domain:DemoItem>
94+
<domain:DemoItem Name="Trees">
95+
<domain:DemoItem.Content>
96+
<wpfExample:Trees />
97+
</domain:DemoItem.Content>
98+
</domain:DemoItem>
99+
<domain:DemoItem Name="Grids">
100+
<domain:DemoItem.Content>
101+
<wpfExample:Grids>
102+
<wpfExample:Grids.DataContext>
103+
<domain:ListsAndGridsViewModel />
104+
</wpfExample:Grids.DataContext>
105+
</wpfExample:Grids>
106+
</domain:DemoItem.Content>
107+
</domain:DemoItem>
108+
<domain:DemoItem Name="Menus &amp; Tool Bars">
109+
<domain:DemoItem.Content>
110+
<wpfExample:MenusAndToolBars />
111+
</domain:DemoItem.Content>
112+
</domain:DemoItem>
113+
<domain:DemoItem Name="Progress Indicators">
114+
<domain:DemoItem.Content>
115+
<wpfExample:Progress />
116+
</domain:DemoItem.Content>
117+
</domain:DemoItem>
118+
<domain:DemoItem Name="Dialogs">
119+
<domain:DemoItem.Content>
120+
<wpfExample:Dialogs>
121+
<wpfExample:Dialogs.DataContext>
122+
<domain:DialogsViewModel />
123+
</wpfExample:Dialogs.DataContext>
124+
</wpfExample:Dialogs>
125+
</domain:DemoItem.Content>
126+
</domain:DemoItem>
127+
<domain:DemoItem Name="Shadows">
128+
<domain:DemoItem.Content>
129+
<wpfExample:Shadows />
130+
</domain:DemoItem.Content>
131+
</domain:DemoItem>
132+
</ListBox>
133+
</StackPanel>
134+
</wpf:DrawerHost.LeftDrawerContent>
135+
<DockPanel>
136+
<wpf:ColorZone Padding="16" wpf:ShadowAssist.ShadowDepth="Depth2"
137+
Mode="PrimaryMid" DockPanel.Dock="Top">
138+
<DockPanel>
139+
<ToggleButton Style="{StaticResource MaterialDesignHamburgerToggleButton}" IsChecked="False"
140+
x:Name="MenuToggleButton"/>
141+
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22">Material Design In XAML Toolkit</TextBlock>
142+
</DockPanel>
143+
</wpf:ColorZone>
144+
<ContentControl Margin="16" Content="{Binding ElementName=DemoItemsListBox, Path=SelectedItem.Content}" />
145+
</DockPanel>
146+
</wpf:DrawerHost>
139147
</wpf:DialogHost>
140148
</Window>

MainDemo.Wpf/MainWindow.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Windows;
2+
using System.Windows.Input;
23

34
namespace MaterialDesignColors.WpfExample
45
{
@@ -11,5 +12,10 @@ public MainWindow()
1112
{
1213
InitializeComponent();
1314
}
15+
16+
private void UIElement_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
17+
{
18+
MenuToggleButton.IsChecked = false;
19+
}
1420
}
1521
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows.Data;
8+
9+
namespace MaterialDesignThemes.Wpf.Converters
10+
{
11+
public class DrawerOffsetConverter : IValueConverter
12+
{
13+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
14+
{
15+
var d = value as double? ?? 0;
16+
if (double.IsInfinity(d) || double.IsNaN(d)) d = 0;
17+
return 0 - d;
18+
}
19+
20+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
21+
{
22+
throw new NotImplementedException();
23+
}
24+
}
25+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System;
2+
using System.Windows;
3+
using System.Windows.Controls;
4+
using System.Windows.Input;
5+
using System.Windows.Media;
6+
7+
namespace MaterialDesignThemes.Wpf
8+
{
9+
[TemplateVisualState(GroupName = TemplateAllDrawersGroupName, Name = TemplateAllDrawersAllClosedStateName)]
10+
[TemplateVisualState(GroupName = TemplateAllDrawersGroupName, Name = TemplateAllDrawersAnyOpenStateName)]
11+
[TemplateVisualState(GroupName = TemplateLeftDrawerGroupName, Name = TemplateLeftClosedStateName)]
12+
[TemplateVisualState(GroupName = TemplateLeftDrawerGroupName, Name = TemplateLeftOpenStateName)]
13+
[TemplatePart(Name = TemplateContentCoverPartName, Type = typeof(FrameworkElement))]
14+
public class DrawerHost : ContentControl
15+
{
16+
public const string TemplateAllDrawersGroupName = "AllDrawers";
17+
public const string TemplateAllDrawersAllClosedStateName = "AllClosed";
18+
public const string TemplateAllDrawersAnyOpenStateName = "AnyOpen";
19+
public const string TemplateLeftDrawerGroupName = "LeftDrawer";
20+
public const string TemplateLeftClosedStateName = "LeftDrawerClosed";
21+
public const string TemplateLeftOpenStateName = "LeftDrawerOpen";
22+
23+
public const string TemplateContentCoverPartName = "PART_ContentCover";
24+
25+
private FrameworkElement _templateContentCoverElement;
26+
27+
static DrawerHost()
28+
{
29+
DefaultStyleKeyProperty.OverrideMetadata(typeof(DrawerHost), new FrameworkPropertyMetadata(typeof(DrawerHost)));
30+
}
31+
32+
public static readonly DependencyProperty LeftDrawerContentProperty = DependencyProperty.Register(
33+
"LeftDrawerContent", typeof (object), typeof (DrawerHost), new PropertyMetadata(default(object)));
34+
35+
public object LeftDrawerContent
36+
{
37+
get { return (object) GetValue(LeftDrawerContentProperty); }
38+
set { SetValue(LeftDrawerContentProperty, value); }
39+
}
40+
41+
public static readonly DependencyProperty LeftDrawerContentTemplateProperty = DependencyProperty.Register(
42+
"LeftDrawerContentTemplate", typeof (DataTemplate), typeof (DrawerHost), new PropertyMetadata(default(DataTemplate)));
43+
44+
public DataTemplate LeftDrawerContentTemplate
45+
{
46+
get { return (DataTemplate) GetValue(LeftDrawerContentTemplateProperty); }
47+
set { SetValue(LeftDrawerContentTemplateProperty, value); }
48+
}
49+
50+
public static readonly DependencyProperty LeftDrawerContentTemplateSelectorProperty = DependencyProperty.Register(
51+
"LeftDrawerContentTemplateSelector", typeof (DataTemplateSelector), typeof (DrawerHost), new PropertyMetadata(default(DataTemplateSelector)));
52+
53+
public DataTemplateSelector LeftDrawerContentTemplateSelector
54+
{
55+
get { return (DataTemplateSelector) GetValue(LeftDrawerContentTemplateSelectorProperty); }
56+
set { SetValue(LeftDrawerContentTemplateSelectorProperty, value); }
57+
}
58+
59+
public static readonly DependencyProperty LeftDrawerContentStringFormatProperty = DependencyProperty.Register(
60+
"LeftDrawerContentStringFormat", typeof (string), typeof (DrawerHost), new PropertyMetadata(default(string)));
61+
62+
public string LeftDrawerContentStringFormat
63+
{
64+
get { return (string) GetValue(LeftDrawerContentStringFormatProperty); }
65+
set { SetValue(LeftDrawerContentStringFormatProperty, value); }
66+
}
67+
68+
public static readonly DependencyProperty LeftDrawerBackgroundProperty = DependencyProperty.Register(
69+
"LeftDrawerBackground", typeof (Brush), typeof (DrawerHost), new PropertyMetadata(default(Brush)));
70+
71+
public Brush LeftDrawerBackground
72+
{
73+
get { return (Brush) GetValue(LeftDrawerBackgroundProperty); }
74+
set { SetValue(LeftDrawerBackgroundProperty, value); }
75+
}
76+
77+
public static readonly DependencyProperty IsLeftDrawerOpenProperty = DependencyProperty.Register(
78+
"IsLeftDrawerOpen", typeof (bool), typeof (DrawerHost), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsLeftDrawerOpenPropertyChangedCallback));
79+
80+
public bool IsLeftDrawerOpen
81+
{
82+
get { return (bool) GetValue(IsLeftDrawerOpenProperty); }
83+
set { SetValue(IsLeftDrawerOpenProperty, value); }
84+
}
85+
86+
public override void OnApplyTemplate()
87+
{
88+
if (_templateContentCoverElement != null)
89+
_templateContentCoverElement.PreviewMouseLeftButtonUp += TemplateContentCoverElementOnPreviewMouseLeftButtonUp;
90+
91+
base.OnApplyTemplate();
92+
93+
_templateContentCoverElement = GetTemplateChild(TemplateContentCoverPartName) as FrameworkElement;
94+
if (_templateContentCoverElement != null)
95+
_templateContentCoverElement.PreviewMouseLeftButtonUp += TemplateContentCoverElementOnPreviewMouseLeftButtonUp;
96+
97+
98+
UpdateVisualStates(false);
99+
}
100+
101+
private void TemplateContentCoverElementOnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs mouseButtonEventArgs)
102+
{
103+
SetCurrentValue(IsLeftDrawerOpenProperty, false);
104+
}
105+
106+
private void UpdateVisualStates(bool useTransitions)
107+
{
108+
var anyOpen = IsLeftDrawerOpen;
109+
110+
VisualStateManager.GoToState(this,
111+
!anyOpen ? TemplateAllDrawersAllClosedStateName : TemplateAllDrawersAnyOpenStateName, useTransitions);
112+
113+
VisualStateManager.GoToState(this,
114+
IsLeftDrawerOpen ? TemplateLeftOpenStateName : TemplateLeftClosedStateName, useTransitions);
115+
}
116+
117+
private static void IsLeftDrawerOpenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
118+
{
119+
((DrawerHost)dependencyObject).UpdateVisualStates(true);
120+
}
121+
122+
}
123+
}

0 commit comments

Comments
 (0)