Browse Source

Draw DropDown selected content using VisualBrush

If content is a control. Fixes #87.
pull/111/head
Steven Kirk 11 years ago
parent
commit
52dd3d1b6b
  1. 39
      src/Perspex.Controls/DropDown.cs
  2. 5
      src/Perspex.SceneGraph/Rendering/RendererBase.cs
  3. 4
      src/Perspex.Themes.Default/DropDownStyle.cs
  4. 3
      src/Perspex.Themes.Default/ListBoxItemStyle.cs

39
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<bool> IsDropDownOpenProperty =
PerspexProperty.Register<DropDown, bool>("IsDropDownOpen");
public static readonly PerspexProperty<object> SelectionBoxItemProperty =
PerspexProperty.Register<DropDown, object>("SelectionBoxItem");
private Popup popup;
static DropDown()
{
FocusableProperty.OverrideDefaultValue<DropDown>(true);
SelectedItemProperty.Changed.AddClassHandler<DropDown>(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<ListBoxItem>(this);
@ -126,19 +137,27 @@ namespace Perspex.Controls
}
}
private void SetContentParent(Tuple<object, object> 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;
}
}
}

5
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;
}
/// <summary>
/// Renders the specified visual with the specified transform.
/// Renders the specified visual with the specified transform and clip.
/// </summary>
/// <param name="visual">The visual to render.</param>
/// <param name="handle">An optional platform-specific handle.</param>
@ -51,8 +52,6 @@ namespace Perspex.Rendering
{
this.Render(visual, context, Matrix.Identity, transform);
}
++this.RenderCount;
}
/// <summary>

4
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,

3
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)),
},
},
});

Loading…
Cancel
Save