Browse Source

DropDown shouldn't be an IContentControl.

pull/396/head
Steven Kirk 11 years ago
parent
commit
adee104ad9
  1. 54
      src/Perspex.Controls/DropDown.cs
  2. 6
      src/Perspex.Themes.Default/DropDown.paml
  3. 2
      tests/Perspex.Controls.UnitTests/DropDownTests.cs

54
src/Perspex.Controls/DropDown.cs

@ -16,23 +16,20 @@ namespace Perspex.Controls
/// <summary> /// <summary>
/// A drop-down list control. /// A drop-down list control.
/// </summary> /// </summary>
public class DropDown : SelectingItemsControl, IContentControl public class DropDown : SelectingItemsControl
{ {
public static readonly PerspexProperty<object> ContentProperty = /// <summary>
ContentControl.ContentProperty.AddOwner<DropDown>(); /// Defines the <see cref="IsDropDownOpen"/> property.
/// </summary>
public static readonly PerspexProperty<HorizontalAlignment> HorizontalContentAlignmentProperty =
ContentControl.HorizontalContentAlignmentProperty.AddOwner<DropDown>();
public static readonly PerspexProperty<VerticalAlignment> VerticalContentAlignmentProperty =
ContentControl.VerticalContentAlignmentProperty.AddOwner<DropDown>();
public static readonly PerspexProperty<bool> IsDropDownOpenProperty = public static readonly PerspexProperty<bool> IsDropDownOpenProperty =
PerspexProperty.RegisterDirect<DropDown, bool>( PerspexProperty.RegisterDirect<DropDown, bool>(
nameof(IsDropDownOpen), nameof(IsDropDownOpen),
o => o.IsDropDownOpen, o => o.IsDropDownOpen,
(o, v) => o.IsDropDownOpen = v); (o, v) => o.IsDropDownOpen = v);
/// <summary>
/// Defines the <see cref="SelectionBoxItem"/> property.
/// </summary>
public static readonly PerspexProperty<object> SelectionBoxItemProperty = public static readonly PerspexProperty<object> SelectionBoxItemProperty =
PerspexProperty.RegisterDirect<DropDown, object>("SelectionBoxItem", o => o.SelectionBoxItem); PerspexProperty.RegisterDirect<DropDown, object>("SelectionBoxItem", o => o.SelectionBoxItem);
@ -40,58 +37,47 @@ namespace Perspex.Controls
private Popup _popup; private Popup _popup;
private object _selectionBoxItem; private object _selectionBoxItem;
/// <summary>
/// Initializes static members of the <see cref="DropDown"/> class.
/// </summary>
static DropDown() static DropDown()
{ {
FocusableProperty.OverrideDefaultValue<DropDown>(true); FocusableProperty.OverrideDefaultValue<DropDown>(true);
SelectedItemProperty.Changed.AddClassHandler<DropDown>(x => x.SelectedItemChanged); SelectedItemProperty.Changed.AddClassHandler<DropDown>(x => x.SelectedItemChanged);
} }
public DropDown() /// <summary>
{ /// Gets or sets a value indicating whether the dropdown is currently open.
Bind(ContentProperty, GetObservable(SelectedItemProperty)); /// </summary>
}
public object Content
{
get { return GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
public HorizontalAlignment HorizontalContentAlignment
{
get { return GetValue(HorizontalContentAlignmentProperty); }
set { SetValue(HorizontalContentAlignmentProperty, value); }
}
public VerticalAlignment VerticalContentAlignment
{
get { return GetValue(VerticalContentAlignmentProperty); }
set { SetValue(VerticalContentAlignmentProperty, value); }
}
public bool IsDropDownOpen public bool IsDropDownOpen
{ {
get { return _isDropDownOpen; } get { return _isDropDownOpen; }
set { SetAndRaise(IsDropDownOpenProperty, ref _isDropDownOpen, value); } set { SetAndRaise(IsDropDownOpenProperty, ref _isDropDownOpen, value); }
} }
/// <summary>
/// Gets or sets the item to display as the control's content.
/// </summary>
protected object SelectionBoxItem protected object SelectionBoxItem
{ {
get { return _selectionBoxItem; } get { return _selectionBoxItem; }
set { SetAndRaise(SelectionBoxItemProperty, ref _selectionBoxItem, value); } set { SetAndRaise(SelectionBoxItemProperty, ref _selectionBoxItem, value); }
} }
/// <inheritdoc/>
protected override IItemContainerGenerator CreateItemContainerGenerator() protected override IItemContainerGenerator CreateItemContainerGenerator()
{ {
return new ItemContainerGenerator<DropDownItem>(this, DropDownItem.ContentProperty); return new ItemContainerGenerator<DropDownItem>(this, DropDownItem.ContentProperty);
} }
/// <inheritdoc/>
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{ {
base.OnAttachedToLogicalTree(e); base.OnAttachedToLogicalTree(e);
this.UpdateSelectionBoxItem(this.SelectedItem); this.UpdateSelectionBoxItem(this.SelectedItem);
} }
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e) protected override void OnKeyDown(KeyEventArgs e)
{ {
base.OnKeyDown(e); base.OnKeyDown(e);
@ -112,6 +98,7 @@ namespace Perspex.Controls
} }
} }
/// <inheritdoc/>
protected override void OnPointerPressed(PointerPressEventArgs e) protected override void OnPointerPressed(PointerPressEventArgs e)
{ {
if (!IsDropDownOpen && ((IVisual)e.Source).GetVisualRoot() != typeof(PopupRoot)) if (!IsDropDownOpen && ((IVisual)e.Source).GetVisualRoot() != typeof(PopupRoot))
@ -132,6 +119,7 @@ namespace Perspex.Controls
base.OnPointerPressed(e); base.OnPointerPressed(e);
} }
/// <inheritdoc/>
protected override void OnTemplateApplied(TemplateAppliedEventArgs e) protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
{ {
if (_popup != null) if (_popup != null)

6
src/Perspex.Themes.Default/DropDown.paml

@ -2,8 +2,6 @@
<Style Selector="DropDown"> <Style Selector="DropDown">
<Setter Property="BorderBrush" Value="#ff707070"/> <Setter Property="BorderBrush" Value="#ff707070"/>
<Setter Property="BorderThickness" Value="2"/> <Setter Property="BorderThickness" Value="2"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="4"/> <Setter Property="Padding" Value="4"/>
<Setter Property="Template"> <Setter Property="Template">
<ControlTemplate> <ControlTemplate>
@ -13,8 +11,8 @@
<Grid ColumnDefinitions="*,Auto"> <Grid ColumnDefinitions="*,Auto">
<ContentPresenter Content="{TemplateBinding SelectionBoxItem}" <ContentPresenter Content="{TemplateBinding SelectionBoxItem}"
Margin="{TemplateBinding Padding}" Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" HorizontalAlignment="Center"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> VerticalAlignment="Center"/>
<ToggleButton Name="toggle" <ToggleButton Name="toggle"
BorderThickness="0" BorderThickness="0"
Background="Transparent" Background="Transparent"

2
tests/Perspex.Controls.UnitTests/DropDownTests.cs

@ -40,7 +40,7 @@ namespace Perspex.Controls.UnitTests
new ContentControl new ContentControl
{ {
Name = "contentControl", Name = "contentControl",
[~ContentPresenter.ContentProperty] = parent[~DropDown.ContentProperty], [~ContentPresenter.ContentProperty] = parent[~DropDown.SelectionBoxItemProperty],
}, },
new ToggleButton new ToggleButton
{ {

Loading…
Cancel
Save