ui (control): Add ExpandedContentSelector for modal section-expansion

This commit is contained in:
John Colvin committed 2022-06-01 10:11:26 -04:00
1 parent 5b3925fae0
commit 2784e7652d
3 files changed
+239

No files matched your search

@@ -0,0 +1,112 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="clr-namespace:mbts.ui"
xmlns:c="clr-namespace:mbts.ui.controls"
xmlns:cv="clr-namespace:mbts.ui.converters">
<ControlTemplate x:Key="ExpandedContentSelector.Template" TargetType="c:ExpandedContentSelector">
<Border
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}">
<ItemsPresenter />
</Border>
</ControlTemplate>
<Style
x:Key="ExpandedContentSelector.ItemHeaderStyle"
TargetType="ToggleButton"
BasedOn="{StaticResource LargeMenuToggleButton}">
<Setter Property="Padding" Value="12,10" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="ToolTip" Value="{x:Null}" />
<Setter Property="Focusable" Value="False" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock FontSize="13" Text="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger
Binding="{Binding
Path='(Validation.HasError)',
RelativeSource={RelativeSource Self}}"
Value="{u:True}">
<Setter Property="Foreground" Value="{x:Static u:BTSColors.error_foreground_brush}" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="ToolTip">
<Setter.Value>
<TextBlock>Invalid</TextBlock>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="ExpandedContentSelector.ContentContainerStyle" TargetType="ContentControl">
<Setter Property="Height" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="ExpandedContentSelectorItem.Template" TargetType="c:ExpandedContentSelectorItem">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
Padding="{TemplateBinding Padding}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ToggleButton
Name="PART_SelectionToggleButton"
Content="{Binding Header, RelativeSource={RelativeSource TemplatedParent}}"
ContentTemplate="{Binding HeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}"
ContentTemplateSelector="{Binding HeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}"
IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
Style="{Binding ItemHeaderStyle, RelativeSource={RelativeSource AncestorType=c:ExpandedContentSelector}}">
</ToggleButton>
<ContentControl
Grid.Row="1"
Style="{StaticResource ExpandedContentSelector.ContentContainerStyle}"
VerticalAlignment="Top">
<ContentControl
Name="PART_Content"
Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"
ContentTemplate="{Binding ContentTemplate, RelativeSource={RelativeSource TemplatedParent}}"
ContentTemplateSelector="{Binding ContentTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}" />
</ContentControl>
</Grid>
</Border>
</ControlTemplate>
<Style TargetType="c:ExpandedContentSelectorItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Padding" Value="0" />
<Setter Property="Template" Value="{StaticResource ExpandedContentSelectorItem.Template}" />
</Style>
<Style TargetType="c:ExpandedContentSelector">
<Setter Property="Template" Value="{StaticResource ExpandedContentSelector.Template}" />
<Setter Property="ItemHeaderStyle" Value="{StaticResource ExpandedContentSelector.ItemHeaderStyle}" />
<Setter Property="SelectionMode" Value="Single" />
</Style>
</ResourceDictionary>
@@ -0,0 +1,38 @@
#nullable enable
namespace mbts.ui.controls {
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
/// <summary>
/// An ItemsControl implementation that displays each item with a ToggleButton header control
/// that is used to expand/collapse the item's main content
/// </summary>
/// <remarks>
/// Only one item may be expanded at a time.
/// <para/>
/// TODO: Derive from Selector instead of ItemsControl. The tricky part will be syncing
/// this class's 'SelectedItem' prop with <see cref="RadioButtonGroupManager.SelectedItem"/>
/// </remarks>
class ExpandedContentSelector : ListBox {
static ExpandedContentSelector() {
DefaultStyleKeyProperty.OverrideMetadata(
typeof(ExpandedContentSelector),
new FrameworkPropertyMetadata(typeof(ExpandedContentSelector)));
}
#region ItemHeaderStyle
public static readonly DependencyProperty ItemHeaderStyleProperty =
WPFHelper.create_dp<Style, ExpandedContentSelector>(nameof(ItemHeaderStyle));
public Style? ItemHeaderStyle {
get => GetValue(ItemHeaderStyleProperty) as Style;
set => SetValue(ItemHeaderStyleProperty, value);
}
#endregion
protected override DependencyObject GetContainerForItemOverride() =>
new ExpandedContentSelectorItem();
}
}
@@ -0,0 +1,89 @@
#nullable enable
namespace mbts.ui.controls {
using mbts.ui.extensions;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Threading;
class ExpandedContentSelectorItem : ListBoxItem {
FrameworkElement? _content_view;
static ExpandedContentSelectorItem() =>
DefaultStyleKeyProperty.OverrideMetadata(
typeof(ExpandedContentSelectorItem),
new FrameworkPropertyMetadata(typeof(ExpandedContentSelectorItem)));
public ExpandedContentSelectorItem() {
Selected += on_selected;
Unselected += on_unselected;
void on_selected(object sender, RoutedEventArgs e) =>
// Use InvokeAsync as a little hack to make the collapse animation of the previously
// selected item happen before the expand animation of the newly selected item (this)
Dispatcher.InvokeAsync(
() => _set_is_expanded(IsSelected), DispatcherPriority.ApplicationIdle);
void on_unselected(object sender, RoutedEventArgs e) =>
_set_is_expanded(false);
}
public static readonly DependencyProperty HeaderProperty =
HeaderedContentControl.HeaderProperty.AddOwner(typeof(ExpandedContentSelectorItem));
[Bindable(true), Category("Content")]
[Localizability(LocalizationCategory.Label)]
public object Header {
get => GetValue(HeaderProperty);
set => SetValue(HeaderProperty, value);
}
public static readonly DependencyProperty HeaderTemplateProperty =
HeaderedContentControl.HeaderTemplateProperty.AddOwner(
typeof(ExpandedContentSelectorItem));
[Bindable(true), Category("Content")]
public DataTemplate HeaderTemplate {
get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
set { SetValue(HeaderTemplateProperty, value); }
}
public static readonly DependencyProperty HeaderTemplateSelectorProperty =
HeaderedContentControl.HeaderTemplateSelectorProperty.AddOwner(
typeof(ExpandedContentSelectorItem));
[Bindable(true), Category("Content")]
public DataTemplateSelector HeaderTemplateSelector {
get { return (DataTemplateSelector)GetValue(HeaderTemplateSelectorProperty); }
set { SetValue(HeaderTemplateSelectorProperty, value); }
}
public override void OnApplyTemplate() {
if (this.try_get_template_child("PART_Content", out _content_view)) {
if (_content_view is null) {
// try_get_template_child shouldn't return true when the result is null!
throw new InvalidOperationException(
"Expected template child, 'PART_Content', to be non null");
}
if (IsSelected != (_content_view.ActualHeight != 0)) {
_set_is_expanded(IsSelected);
}
}
}
// Override so that the ToggleButton (i.e., the Header) handles the selection behavior, and
// the base class (ListBoxItem) doesn't intercept mouse clicks from the main content.
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { }
void _set_is_expanded(bool is_expanded) {
if (_content_view is null) {
return;
}
ExpandCollapseAnimator.SetIsExpanded(_content_view, is_expanded);
}
}
}