From 52dd3d1b6bc7389f543ab9e603505b9bb07bd83b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 5 Sep 2015 18:00:49 +0200 Subject: [PATCH] Draw DropDown selected content using VisualBrush If content is a control. Fixes #87. --- src/Perspex.Controls/DropDown.cs | 39 ++++++++++++++----- .../Rendering/RendererBase.cs | 5 +-- src/Perspex.Themes.Default/DropDownStyle.cs | 4 +- .../ListBoxItemStyle.cs | 3 +- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/Perspex.Controls/DropDown.cs b/src/Perspex.Controls/DropDown.cs index 439a62c06d..ecb2099fb9 100644 --- a/src/Perspex.Controls/DropDown.cs +++ b/src/Perspex.Controls/DropDown.cs @@ -10,10 +10,12 @@ namespace Perspex.Controls using System.Linq; using Generators; using Perspex.Controls.Primitives; + using Perspex.Controls.Shapes; using Perspex.Controls.Templates; using Perspex.Input; using Perspex.Layout; - using VisualTree; + using Perspex.Media; + using Perspex.VisualTree; public class DropDown : SelectingItemsControl, IContentControl { @@ -29,16 +31,19 @@ namespace Perspex.Controls public static readonly PerspexProperty IsDropDownOpenProperty = PerspexProperty.Register("IsDropDownOpen"); + public static readonly PerspexProperty SelectionBoxItemProperty = + PerspexProperty.Register("SelectionBoxItem"); + private Popup popup; static DropDown() { FocusableProperty.OverrideDefaultValue(true); + SelectedItemProperty.Changed.AddClassHandler(x => x.SelectedItemChanged); } public DropDown() { - this.GetObservableWithHistory(ContentProperty).Subscribe(this.SetContentParent); this.Bind(ContentProperty, this.GetObservable(DropDown.SelectedItemProperty)); } @@ -66,6 +71,12 @@ namespace Perspex.Controls set { this.SetValue(IsDropDownOpenProperty, value); } } + public object SelectionBoxItem + { + get { return this.GetValue(SelectionBoxItemProperty); } + set { this.SetValue(SelectionBoxItemProperty, value); } + } + protected override IItemContainerGenerator CreateItemContainerGenerator() { return new ItemContainerGenerator(this); @@ -126,19 +137,27 @@ namespace Perspex.Controls } } - private void SetContentParent(Tuple change) + private void SelectedItemChanged(PerspexPropertyChangedEventArgs e) { - var control1 = change.Item1 as Control; - var control2 = change.Item2 as Control; + var control = e.NewValue as IControl; - if (control1 != null) + if (control != null) { - ((ISetLogicalParent)control1).SetParent(null); + this.SelectionBoxItem = new Rectangle + { + Width = control.DesiredSize.Width, + Height = control.DesiredSize.Height, + Fill = new VisualBrush + { + Visual = control, + Stretch = Stretch.None, + AlignmentX = AlignmentX.Left, + } + }; } - - if (control2 != null) + else { - ((ISetLogicalParent)control2).SetParent(this); + this.SelectionBoxItem = e.NewValue; } } } diff --git a/src/Perspex.SceneGraph/Rendering/RendererBase.cs b/src/Perspex.SceneGraph/Rendering/RendererBase.cs index d5b593d5e0..6dd6dbd81c 100644 --- a/src/Perspex.SceneGraph/Rendering/RendererBase.cs +++ b/src/Perspex.SceneGraph/Rendering/RendererBase.cs @@ -35,10 +35,11 @@ namespace Perspex.Rendering public virtual void Render(IVisual visual, IPlatformHandle handle) { this.Render(visual, handle, Matrix.Identity); + ++this.RenderCount; } /// - /// Renders the specified visual with the specified transform. + /// Renders the specified visual with the specified transform and clip. /// /// The visual to render. /// An optional platform-specific handle. @@ -51,8 +52,6 @@ namespace Perspex.Rendering { this.Render(visual, context, Matrix.Identity, transform); } - - ++this.RenderCount; } /// diff --git a/src/Perspex.Themes.Default/DropDownStyle.cs b/src/Perspex.Themes.Default/DropDownStyle.cs index e29abc06b3..f459a317b0 100644 --- a/src/Perspex.Themes.Default/DropDownStyle.cs +++ b/src/Perspex.Themes.Default/DropDownStyle.cs @@ -93,7 +93,7 @@ namespace Perspex.Themes.Default { Name = "contentControl", Margin = new Thickness(3), - [~ContentControl.ContentProperty] = control[~DropDown.ContentProperty], + [~ContentControl.ContentProperty] = control[~DropDown.SelectionBoxItemProperty], [~ContentControl.HorizontalAlignmentProperty] = control[~DropDown.HorizontalContentAlignmentProperty], [~ContentControl.VerticalAlignmentProperty] = control[~DropDown.VerticalContentAlignmentProperty], }, @@ -129,7 +129,7 @@ namespace Perspex.Themes.Default Padding = new Thickness(4), Child = new ItemsPresenter { - [~ListBox.ItemsProperty] = control[~DropDown.ItemsProperty], + [~ItemsPresenter.ItemsProperty] = control[~DropDown.ItemsProperty], } }, PlacementTarget = control, diff --git a/src/Perspex.Themes.Default/ListBoxItemStyle.cs b/src/Perspex.Themes.Default/ListBoxItemStyle.cs index fecf9472b3..e6056d970b 100644 --- a/src/Perspex.Themes.Default/ListBoxItemStyle.cs +++ b/src/Perspex.Themes.Default/ListBoxItemStyle.cs @@ -43,8 +43,7 @@ namespace Perspex.Themes.Default { Setters = new[] { - new Setter(ListBoxItem.BackgroundProperty, new SolidColorBrush(0xff086f9e)), - new Setter(ListBoxItem.ForegroundProperty, Brushes.White), + new Setter(ListBoxItem.BackgroundProperty, new SolidColorBrush(0xffd0d0d0)), }, }, });