// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Themes.Default { using System.Linq; using Perspex.Controls; using Perspex.Layout; using Perspex.Media; using Perspex.Controls.Shapes; using Perspex.Styling; using Perspex.Controls.Presenters; using Perspex.Controls.Primitives; public class TreeViewItemStyle : Styles { public TreeViewItemStyle() { this.AddRange(new[] { new Style(x => x.OfType()) { Setters = new[] { new Setter(Button.TemplateProperty, ControlTemplate.Create(this.Template)), }, }, new Style(x => x.OfType().Template().Id("header")) { Setters = new[] { new Setter(Border.PaddingProperty, new Thickness(2)), }, }, new Style(x => x.OfType().Class(":selected").Template().Id("header")) { Setters = new[] { new Setter(TreeViewItem.BackgroundProperty, new SolidColorBrush(0xff086f9e)), new Setter(TreeViewItem.ForegroundProperty, Brushes.White), }, }, new Style(x => x.OfType().Template().OfType().Class("expander")) { Setters = new[] { new Setter(ToggleButton.TemplateProperty, ControlTemplate.Create(this.ToggleButtonTemplate)), }, }, new Style(x => x.OfType().Template().OfType().Class("expander").Class(":checked").Template().OfType()) { Setters = new[] { new Setter(ToggleButton.RenderTransformProperty, new RotateTransform(90)), }, }, new Style(x => x.OfType().Class(":empty").Template().OfType().Class("expander")) { Setters = new[] { new Setter(ToggleButton.IsVisibleProperty, false), }, }, }); } private Control Template(TreeViewItem control) { return new StackPanel { Children = new Controls { new Grid { ColumnDefinitions = new ColumnDefinitions { new ColumnDefinition(new GridLength(16, GridUnitType.Pixel)), new ColumnDefinition(GridLength.Auto), }, Children = new Controls { new ToggleButton { Classes = new Classes("expander"), [~~ToggleButton.IsCheckedProperty] = control[~TreeViewItem.IsExpandedProperty], }, new Border { Id = "header", [~ContentPresenter.BackgroundProperty] = control[~TreeViewItem.BackgroundProperty], [Grid.ColumnProperty] = 1, Content = new ContentPresenter { [~ContentPresenter.ContentProperty] = control[~TreeViewItem.HeaderProperty], }, } } }, new ItemsPresenter { Margin = new Thickness(24, 0, 0, 0), [~ItemsPresenter.ItemsProperty] = control[~TreeViewItem.ItemsProperty], [~ItemsPresenter.ItemsPanelProperty] = control[~TreeViewItem.ItemsPanelProperty], [~ItemsPresenter.IsVisibleProperty] = control[~TreeViewItem.IsExpandedProperty], } } }; } private Control ToggleButtonTemplate(ToggleButton control) { return new Border { Content = new Path { Fill = Brushes.Black, Stroke = Brushes.Black, StrokeThickness = 1, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Data = StreamGeometry.Parse("M 0 2 L 4 6 L 0 10 Z"), } }; } } }