From 10bc3196bde75047ab17ae8bddf23fd7a0dd6cfd Mon Sep 17 00:00:00 2001 From: Odonno Date: Sun, 11 Dec 2016 17:33:57 +0100 Subject: [PATCH 1/7] feat(Expander): create Expander control --- .../Microsoft.Toolkit.Uwp.SampleApp.csproj | 16 +- .../SamplePages/Expander/Expander.png | Bin 0 -> 3225 bytes .../SamplePages/Expander/ExpanderPage.xaml | 45 +++ .../SamplePages/Expander/ExpanderPage.xaml.cs | 60 +++ .../SamplePages/Expander/ExpanderXaml.bind | 45 +++ .../SamplePages/samples.json | 8 + .../Expander/Expander.Constants.cs | 31 ++ .../Expander/Expander.Properties.cs | 100 +++++ .../Expander/Expander.cs | 107 ++++++ .../Expander/Expander.xaml | 348 ++++++++++++++++++ .../Microsoft.Toolkit.Uwp.UI.Controls.csproj | 8 + .../Themes/Generic.xaml | 1 + 12 files changed, 768 insertions(+), 1 deletion(-) create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/Expander.png create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml.cs create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderXaml.bind create mode 100644 Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Constants.cs create mode 100644 Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Properties.cs create mode 100644 Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs create mode 100644 Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.xaml diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj index 5a4e88729a1..6ef2cd919b7 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj +++ b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj @@ -236,6 +236,7 @@ + @@ -306,7 +307,9 @@ - + + Designer + @@ -352,6 +355,9 @@ DropShadowPanelPage.xaml + + ExpanderPage.xaml + @@ -530,6 +536,14 @@ MSBuild:Compile Designer + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + MSBuild:Compile Designer diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/Expander.png b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/Expander.png new file mode 100644 index 0000000000000000000000000000000000000000..e54a239db35cee0da2d59cbb6532ef564e59cdcf GIT binary patch literal 3225 zcmeAS@N?(olHy`uVBq!ia0y~yU{(NO4rZXpn|0r!fD~hKkh>GZx^prwfgF}}M_)$< zhK>E)e-c@NDj5QNLR^93d3kwr=FB;B=FH{Gm#<#EdiU<#2M-=RefsqI^XD&LzI^lM z&D*zc-@kwV;ll@@;!!Xf0>dc;ex3cW1n3{m0*}aI1_s%75N7Ou`Y{zKC|TkfQ4*Y= zR#Ki=l*&+$n3-3imzP?iV4`QBXK3WilKqB(f#a{Ii(^Pd+}oQQi)2C>TmmCcI5Exq z|35}%55uYpb2(F@9u!ZyyYkk8=JM>aGWmIp<(w={4hkHj#9$7CJDg8mUp`*dx^a5( zHvPMtPhL*%|H|I8ci!e&PlZ1{EKH6H0>lz#jW+YHbEhkx@nNd0*%5x?CWrB42RXTS zqD5wh&b;6&T0dd4V4B-`h3{OJ(urxpC(b&ZbDJT_3K0)$yrcCbr)6fOLOEDk{6sHU znyn}nWYB2`J#LULsS}}!KpS$HcWQuC7b<{dUps69nfE*+^ b=g;jK&#E@8a_#Q9#sCDKu6{1-oD!M<+^X_= literal 0 HcmV?d00001 diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml new file mode 100644 index 00000000000..2bc5fae5c01 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml.cs new file mode 100644 index 00000000000..5c7d6f57161 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml.cs @@ -0,0 +1,60 @@ +// ****************************************************************** +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the MIT License (MIT). +// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. +// ****************************************************************** + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Microsoft.Toolkit.Uwp.SampleApp.Models; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Navigation; + +namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages +{ + /// + /// A page that shows how to use the Expander control. + /// + public sealed partial class ExpanderPage : Page + { + /// + /// Initializes a new instance of the class. + /// + public ExpanderPage() + { + InitializeComponent(); + } + + /// + /// Invoked when the Page is loaded and becomes the current source of a parent Frame. + /// + /// Event data that can be examined by overriding code. The event data is representative of the pending navigation that will load the current Page. Usually the most relevant property to examine is Parameter. + protected override void OnNavigatedTo(NavigationEventArgs e) + { + base.OnNavigatedTo(e); + + var propertyDesc = e.Parameter as PropertyDescriptor; + + if (propertyDesc != null) + { + DataContext = propertyDesc.Expando; + } + } + } +} diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderXaml.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderXaml.bind new file mode 100644 index 00000000000..5e23b6a28c1 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderXaml.bind @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json index 603ffa41507..5cea00b24ac 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json @@ -122,6 +122,14 @@ "CodeUrl": "https://github.com/Microsoft/UWPCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI.Controls/TextBoxMask", "XamlCodeFile": "TextBoxMask.bind", "Icon": "/SamplePages/TextBoxMask/TextBoxMask.png" + }, + { + "Name": "Expander", + "Type": "ExpanderPage", + "About": "Expander control allows user to show/hide content based on a boolean state.", + "CodeUrl": "https://github.com/Microsoft/UWPCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI.Controls/Expander", + "XamlCodeFile": "ExpanderXaml.bind", + "Icon": "/SamplePages/Expander/Expander.png" } ] }, diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Constants.cs b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Constants.cs new file mode 100644 index 00000000000..60a275af621 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Constants.cs @@ -0,0 +1,31 @@ +// ****************************************************************** +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the MIT License (MIT). +// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. +// ****************************************************************** + +namespace Microsoft.Toolkit.Uwp.UI.Controls +{ + /// + /// The control allows user to show/hide content based on a boolean state + /// + public partial class Expander + { + public const string GroupContent = "ContentStates"; + public const string StateContentExpanded = "ContentExpanded"; + public const string StateContentCollapsed = "ContentCollapsed"; + + public const string ExpanderToggleButtonPart = "PART_ExpanderToggleButton"; + public const string HeaderButtonPart = "PART_HeaderButton"; + public const string MainContentRowPart = "PART_MainContentRow"; + + public const string OpenCloseStoryboardPart = "PART_OpenCloseStoryboard"; + public const string OpenCloseAnimationPart = "PART_OpenCloseAnimation"; + } +} \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Properties.cs b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Properties.cs new file mode 100644 index 00000000000..2cbbdd1e027 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Properties.cs @@ -0,0 +1,100 @@ +// ****************************************************************** +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the MIT License (MIT). +// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. +// ****************************************************************** + +using System.Windows.Input; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Markup; + +namespace Microsoft.Toolkit.Uwp.UI.Controls +{ + /// + /// The control allows user to show/hide content based on a boolean state + /// + public partial class Expander : Control + { + public static readonly DependencyProperty ExpanderToggleButtonStyleProperty = + DependencyProperty.Register("ExpanderToggleButtonStyle", typeof(Style), typeof(Expander), new PropertyMetadata(null)); + + public static readonly DependencyProperty HeaderButtonStyleProperty = + DependencyProperty.Register("HeaderButtonStyle", typeof(Style), typeof(Expander), new PropertyMetadata(null)); + + public static readonly DependencyProperty HeaderButtonContentProperty = + DependencyProperty.Register("HeaderButtonContent", typeof(object), typeof(Expander), new PropertyMetadata(null)); + + public static readonly DependencyProperty HeaderButtonCommandProperty = + DependencyProperty.Register("HeaderButtonCommand", typeof(ICommand), typeof(Expander), new PropertyMetadata(null)); + + public static readonly DependencyProperty ContentProperty = + DependencyProperty.Register("Content", typeof(object), typeof(Expander), new PropertyMetadata(null)); + + public static readonly DependencyProperty IsExpandedProperty = + DependencyProperty.Register("IsExpanded", typeof(bool), typeof(Expander), new PropertyMetadata(false, OnIsExpandedPropertyChanged)); + + public Style ExpanderToggleButtonStyle + { + get { return (Style)GetValue(ExpanderToggleButtonStyleProperty); } + set { SetValue(ExpanderToggleButtonStyleProperty, value); } + } + + public Style HeaderButtonStyle + { + get { return (Style)GetValue(HeaderButtonStyleProperty); } + set { SetValue(HeaderButtonStyleProperty, value); } + } + + public object HeaderButtonContent + { + get { return GetValue(HeaderButtonContentProperty); } + set { SetValue(HeaderButtonContentProperty, value); } + } + + public ICommand HeaderButtonCommand + { + get { return (ICommand)GetValue(HeaderButtonCommandProperty); } + set { SetValue(HeaderButtonCommandProperty, value); } + } + + public object Content + { + get { return GetValue(ContentProperty); } + set { SetValue(ContentProperty, value); } + } + + public bool IsExpanded + { + get { return (bool)GetValue(IsExpandedProperty); } + set { SetValue(IsExpandedProperty, value); } + } + + private static void OnIsExpandedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var expander = d as Expander; + if (expander == null || expander._expanderButton == null) + { + return; + } + + var isExpanded = (bool)e.NewValue; + expander._expanderButton.IsChecked = isExpanded; + if (isExpanded) + { + expander.ExpandControl(); + } + else + { + expander.CollapseControl(); + } + } + } +} \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs new file mode 100644 index 00000000000..eda99737a8f --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs @@ -0,0 +1,107 @@ +// ****************************************************************** +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the MIT License (MIT). +// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. +// ****************************************************************** + +using System.Windows.Input; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Markup; + +namespace Microsoft.Toolkit.Uwp.UI.Controls +{ + /// + /// The control allows user to show/hide content based on a boolean state + /// + [TemplateVisualState(Name = StateContentExpanded, GroupName = GroupContent)] + [TemplateVisualState(Name = StateContentCollapsed, GroupName = GroupContent)] + [TemplatePart(Name = ExpanderToggleButtonPart, Type = typeof(ToggleButton))] + [TemplatePart(Name = HeaderButtonPart, Type = typeof(ButtonBase))] + [ContentProperty(Name = "Content")] + public partial class Expander : Control + { + private ToggleButton _expanderButton; + private ButtonBase _headerButton; + + private RowDefinition _mainContentRow; + + public Expander() + { + DefaultStyleKey = typeof(Expander); + } + + protected override void OnApplyTemplate() + { + _expanderButton = GetTemplateChild(ExpanderToggleButtonPart) as ToggleButton; + _headerButton = GetTemplateChild(HeaderButtonPart) as ButtonBase; + _mainContentRow = GetTemplateChild(MainContentRowPart) as RowDefinition; + + if (_expanderButton != null) + { + _expanderButton.Checked += OnExpanderButtonChecked; + _expanderButton.Unchecked += OnExpanderButtonUnChecked; + _expanderButton.IsChecked = IsExpanded; + if (IsExpanded) + { + ExpandControl(); + } + else + { + CollapseControl(); + } + } + + if (_headerButton != null) + { + _headerButton.Click += OnHeaderButtonClick; + } + } + + private void OnHeaderButtonClick(object sender, RoutedEventArgs e) + { + HeaderButtonCommand?.Execute(null); + } + + private void ExpandControl() + { + if (_mainContentRow == null || !_mainContentRow.Height.Value.Equals(0d)) + { + return; + } + + VisualStateManager.GoToState(this, StateContentExpanded, true); + _mainContentRow.Height = new GridLength(1, GridUnitType.Auto); + } + + private void CollapseControl() + { + if (_mainContentRow == null || _mainContentRow.Height.Value.Equals(0d)) + { + return; + } + + VisualStateManager.GoToState(this, StateContentCollapsed, true); + _mainContentRow.Height = new GridLength(0d); + } + + private void OnExpanderButtonUnChecked(object sender, RoutedEventArgs e) + { + IsExpanded = false; + CollapseControl(); + } + + private void OnExpanderButtonChecked(object sender, RoutedEventArgs e) + { + IsExpanded = true; + ExpandControl(); + } + } +} \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.xaml b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.xaml new file mode 100644 index 00000000000..10b951be01a --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.xaml @@ -0,0 +1,348 @@ + + + + + + + + \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Microsoft.Toolkit.Uwp.UI.Controls.csproj b/Microsoft.Toolkit.Uwp.UI.Controls/Microsoft.Toolkit.Uwp.UI.Controls.csproj index 6e268f8276f..58f9e1ca3c8 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/Microsoft.Toolkit.Uwp.UI.Controls.csproj +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Microsoft.Toolkit.Uwp.UI.Controls.csproj @@ -54,6 +54,9 @@ + + + @@ -116,6 +119,11 @@ MSBuild:Compile PreserveNewest + + Designer + MSBuild:Compile + PreserveNewest + Designer MSBuild:Compile diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Themes/Generic.xaml b/Microsoft.Toolkit.Uwp.UI.Controls/Themes/Generic.xaml index dafc3151b9e..db0de00bc52 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/Themes/Generic.xaml +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Themes/Generic.xaml @@ -13,6 +13,7 @@ + From 4b974049595eec433883ea2d9fb8ecc486b8e282 Mon Sep 17 00:00:00 2001 From: Odonno Date: Wed, 14 Dec 2016 21:13:08 +0100 Subject: [PATCH 2/7] refactor(Expander): improve Expander control --- .../Microsoft.Toolkit.Uwp.SampleApp.csproj | 9 +- .../SamplePages/Expander/ExpanderPage.xaml | 20 +-- .../SamplePages/Expander/ExpanderXaml.bind | 20 +-- .../Expander/Expander.Constants.cs | 11 +- .../Expander/Expander.Events.cs | 32 +++++ .../Expander/Expander.Properties.cs | 62 ++-------- .../Expander/Expander.cs | 65 ++-------- .../Expander/Expander.xaml | 116 +++++------------- .../Microsoft.Toolkit.Uwp.UI.Controls.csproj | 1 + 9 files changed, 106 insertions(+), 230 deletions(-) create mode 100644 Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Events.cs diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj index 6ef2cd919b7..bf372cea084 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj +++ b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj @@ -304,12 +304,11 @@ + - - Designer - + @@ -540,10 +539,6 @@ MSBuild:Compile Designer - - Designer - MSBuild:Compile - MSBuild:Compile Designer diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml index 2bc5fae5c01..97810797d2b 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Expander/ExpanderPage.xaml @@ -9,13 +9,9 @@ - - - - - + - - - - - + - - - - - + - - - - - + public partial class Expander { - public const string GroupContent = "ContentStates"; - public const string StateContentExpanded = "ContentExpanded"; - public const string StateContentCollapsed = "ContentCollapsed"; + public const string GroupContent = "ExpandedStates"; + public const string StateContentExpanded = "Expanded"; + public const string StateContentCollapsed = "Collapsed"; public const string ExpanderToggleButtonPart = "PART_ExpanderToggleButton"; - public const string HeaderButtonPart = "PART_HeaderButton"; + public const string HeaderPart = "PART_Header"; public const string MainContentRowPart = "PART_MainContentRow"; - - public const string OpenCloseStoryboardPart = "PART_OpenCloseStoryboard"; - public const string OpenCloseAnimationPart = "PART_OpenCloseAnimation"; } } \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Events.cs b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Events.cs new file mode 100644 index 00000000000..cfe1f78d137 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Events.cs @@ -0,0 +1,32 @@ +// ****************************************************************** +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the MIT License (MIT). +// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. +// ****************************************************************** + +using System; + +namespace Microsoft.Toolkit.Uwp.UI.Controls +{ + /// + /// The control allows user to show/hide content based on a boolean state + /// + public partial class Expander + { + /// + /// Fires when the expander is opened + /// + public event EventHandler Expanded; + + /// + /// Fires when the expander is closed + /// + public event EventHandler Collapsed; + } +} diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Properties.cs b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Properties.cs index 2cbbdd1e027..6f38c240d00 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Properties.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.Properties.cs @@ -10,65 +10,34 @@ // THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. // ****************************************************************** -using System.Windows.Input; using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Markup; namespace Microsoft.Toolkit.Uwp.UI.Controls { /// /// The control allows user to show/hide content based on a boolean state /// - public partial class Expander : Control + public partial class Expander { - public static readonly DependencyProperty ExpanderToggleButtonStyleProperty = - DependencyProperty.Register("ExpanderToggleButtonStyle", typeof(Style), typeof(Expander), new PropertyMetadata(null)); + public static readonly DependencyProperty HeaderProperty = + DependencyProperty.Register(nameof(Header), typeof(string), typeof(Expander), new PropertyMetadata(null)); - public static readonly DependencyProperty HeaderButtonStyleProperty = - DependencyProperty.Register("HeaderButtonStyle", typeof(Style), typeof(Expander), new PropertyMetadata(null)); - - public static readonly DependencyProperty HeaderButtonContentProperty = - DependencyProperty.Register("HeaderButtonContent", typeof(object), typeof(Expander), new PropertyMetadata(null)); - - public static readonly DependencyProperty HeaderButtonCommandProperty = - DependencyProperty.Register("HeaderButtonCommand", typeof(ICommand), typeof(Expander), new PropertyMetadata(null)); - - public static readonly DependencyProperty ContentProperty = - DependencyProperty.Register("Content", typeof(object), typeof(Expander), new PropertyMetadata(null)); + public static readonly DependencyProperty HeaderTemplateProperty = + DependencyProperty.Register(nameof(HeaderTemplate), typeof(DataTemplate), typeof(Expander), new PropertyMetadata(null)); public static readonly DependencyProperty IsExpandedProperty = - DependencyProperty.Register("IsExpanded", typeof(bool), typeof(Expander), new PropertyMetadata(false, OnIsExpandedPropertyChanged)); - - public Style ExpanderToggleButtonStyle - { - get { return (Style)GetValue(ExpanderToggleButtonStyleProperty); } - set { SetValue(ExpanderToggleButtonStyleProperty, value); } - } - - public Style HeaderButtonStyle - { - get { return (Style)GetValue(HeaderButtonStyleProperty); } - set { SetValue(HeaderButtonStyleProperty, value); } - } + DependencyProperty.Register(nameof(IsExpanded), typeof(bool), typeof(Expander), new PropertyMetadata(false, OnIsExpandedPropertyChanged)); - public object HeaderButtonContent + public string Header { - get { return GetValue(HeaderButtonContentProperty); } - set { SetValue(HeaderButtonContentProperty, value); } + get { return (string)GetValue(HeaderProperty); } + set { SetValue(HeaderProperty, value); } } - public ICommand HeaderButtonCommand + public DataTemplate HeaderTemplate { - get { return (ICommand)GetValue(HeaderButtonCommandProperty); } - set { SetValue(HeaderButtonCommandProperty, value); } - } - - public object Content - { - get { return GetValue(ContentProperty); } - set { SetValue(ContentProperty, value); } + get { return (DataTemplate)GetValue(HeaderTemplateProperty); } + set { SetValue(HeaderTemplateProperty, value); } } public bool IsExpanded @@ -80,13 +49,8 @@ public bool IsExpanded private static void OnIsExpandedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var expander = d as Expander; - if (expander == null || expander._expanderButton == null) - { - return; - } - var isExpanded = (bool)e.NewValue; - expander._expanderButton.IsChecked = isExpanded; + bool isExpanded = (bool)e.NewValue; if (isExpanded) { expander.ExpandControl(); diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs index eda99737a8f..cb45972060c 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.cs @@ -10,7 +10,7 @@ // THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. // ****************************************************************** -using System.Windows.Input; +using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; @@ -24,15 +24,10 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls [TemplateVisualState(Name = StateContentExpanded, GroupName = GroupContent)] [TemplateVisualState(Name = StateContentCollapsed, GroupName = GroupContent)] [TemplatePart(Name = ExpanderToggleButtonPart, Type = typeof(ToggleButton))] - [TemplatePart(Name = HeaderButtonPart, Type = typeof(ButtonBase))] + [TemplatePart(Name = HeaderPart, Type = typeof(Grid))] [ContentProperty(Name = "Content")] - public partial class Expander : Control + public partial class Expander : ContentControl { - private ToggleButton _expanderButton; - private ButtonBase _headerButton; - - private RowDefinition _mainContentRow; - public Expander() { DefaultStyleKey = typeof(Expander); @@ -40,68 +35,24 @@ public Expander() protected override void OnApplyTemplate() { - _expanderButton = GetTemplateChild(ExpanderToggleButtonPart) as ToggleButton; - _headerButton = GetTemplateChild(HeaderButtonPart) as ButtonBase; - _mainContentRow = GetTemplateChild(MainContentRowPart) as RowDefinition; - - if (_expanderButton != null) - { - _expanderButton.Checked += OnExpanderButtonChecked; - _expanderButton.Unchecked += OnExpanderButtonUnChecked; - _expanderButton.IsChecked = IsExpanded; - if (IsExpanded) - { - ExpandControl(); - } - else - { - CollapseControl(); - } - } + base.OnApplyTemplate(); - if (_headerButton != null) + if (!IsExpanded) { - _headerButton.Click += OnHeaderButtonClick; + VisualStateManager.GoToState(this, StateContentCollapsed, false); } } - private void OnHeaderButtonClick(object sender, RoutedEventArgs e) - { - HeaderButtonCommand?.Execute(null); - } - private void ExpandControl() { - if (_mainContentRow == null || !_mainContentRow.Height.Value.Equals(0d)) - { - return; - } - VisualStateManager.GoToState(this, StateContentExpanded, true); - _mainContentRow.Height = new GridLength(1, GridUnitType.Auto); + Expanded?.Invoke(this, new EventArgs()); } private void CollapseControl() { - if (_mainContentRow == null || _mainContentRow.Height.Value.Equals(0d)) - { - return; - } - VisualStateManager.GoToState(this, StateContentCollapsed, true); - _mainContentRow.Height = new GridLength(0d); - } - - private void OnExpanderButtonUnChecked(object sender, RoutedEventArgs e) - { - IsExpanded = false; - CollapseControl(); - } - - private void OnExpanderButtonChecked(object sender, RoutedEventArgs e) - { - IsExpanded = true; - ExpandControl(); + Collapsed?.Invoke(this, new EventArgs()); } } } \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.xaml b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.xaml index 10b951be01a..1d80c864164 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.xaml +++ b/Microsoft.Toolkit.Uwp.UI.Controls/Expander/Expander.xaml @@ -232,91 +232,42 @@ - + + + + + - - - - - -