added two editors

This commit is contained in:
NaBian committed 2020-06-20 18:48:13 +08:00
1 parent 198ef78872
commit f4b1796f98
23 files changed
+345 -60

No files matched your search

@@ -1,7 +1,6 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using HandyControl.Tools;
namespace HandyControl.Controls
{
@@ -18,15 +17,6 @@ namespace HandyControl.Controls
set => SetValue(OrientationProperty, value);
}
public static readonly DependencyProperty ItemStyleSelectorProperty = DependencyProperty.Register(
"ItemStyleSelector", typeof(StyleSelector), typeof(ButtonGroup), new PropertyMetadata(new ButtonGroupItemStyleSelector()));
public StyleSelector ItemStyleSelector
{
get => (StyleSelector)GetValue(ItemStyleSelectorProperty);
set => SetValue(ItemStyleSelectorProperty, value);
}
protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
{
base.OnVisualChildrenChanged(visualAdded, visualRemoved);
@@ -35,7 +25,7 @@ namespace HandyControl.Controls
for (var i = 0; i < count; i++)
{
var item = (ButtonBase)Items[i];
item.Style = ItemStyleSelector?.SelectStyle(item, this);
item.Style = ItemContainerStyleSelector?.SelectStyle(item, this);
}
}
}
@@ -0,0 +1,70 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
using HandyControl.Tools;
namespace HandyControl.Controls
{
public class HorizontalAlignmentPropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.ComboBox
{
Style = ResourceHelper.GetResource<Style>("ComboBoxCapsule"),
ItemsSource = Enum.GetValues(propertyItem.PropertyType),
ItemTemplateSelector = ResourceHelper.GetResource<DataTemplateSelector>("HorizontalAlignmentPathTemplateSelector"),
HorizontalAlignment = HorizontalAlignment.Left
};
public override DependencyProperty GetDependencyProperty() => Selector.SelectedValueProperty;
}
public class HorizontalAlignmentPathTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is HorizontalAlignment horizontalAlignment)
{
var dataTemplate = new DataTemplate
{
DataType = typeof(System.Windows.Controls.ComboBox)
};
var factory = new FrameworkElementFactory(typeof(Path));
factory.SetValue(FrameworkElement.WidthProperty, 12.0);
factory.SetValue(FrameworkElement.HeightProperty, 10.0);
factory.SetBinding(Shape.FillProperty, new Binding(Control.ForegroundProperty.Name)
{
RelativeSource = new RelativeSource
{
AncestorType = typeof(ComboBoxItem)
}
});
switch (horizontalAlignment)
{
case HorizontalAlignment.Left:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResource<Geometry>("AlignLeftGeometry"));
break;
case HorizontalAlignment.Center:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResource<Geometry>("AlignHCenterGeometry"));
break;
case HorizontalAlignment.Right:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResource<Geometry>("AlignRightGeometry"));
break;
case HorizontalAlignment.Stretch:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResource<Geometry>("AlignHStretchGeometry"));
break;
}
dataTemplate.VisualTree = factory;
return dataTemplate;
}
return null;
}
}
}
@@ -3,11 +3,11 @@ using System.Windows.Data;
namespace HandyControl.Controls
{
public abstract class PropertyEditorBase : FrameworkElement
public abstract class PropertyEditorBase : DependencyObject
{
public abstract FrameworkElement CreateElement(PropertyItem propertyItem);
public virtual void CreateBinding(PropertyItem propertyItem, FrameworkElement element) =>
public virtual void CreateBinding(PropertyItem propertyItem, DependencyObject element) =>
BindingOperations.SetBinding(element, GetDependencyProperty(),
new Binding($"({propertyItem.PropertyName})")
{
@@ -0,0 +1,70 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
using HandyControl.Tools;
namespace HandyControl.Controls
{
public class VerticalAlignmentPropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.ComboBox
{
Style = ResourceHelper.GetResource<Style>("ComboBoxCapsule"),
ItemsSource = Enum.GetValues(propertyItem.PropertyType),
ItemTemplateSelector = ResourceHelper.GetResource<DataTemplateSelector>("VerticalAlignmentPathTemplateSelector"),
HorizontalAlignment = HorizontalAlignment.Left
};
public override DependencyProperty GetDependencyProperty() => Selector.SelectedValueProperty;
}
public class VerticalAlignmentPathTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is VerticalAlignment verticalAlignment)
{
var dataTemplate = new DataTemplate
{
DataType = typeof(System.Windows.Controls.ComboBox)
};
var factory = new FrameworkElementFactory(typeof(Path));
factory.SetValue(FrameworkElement.WidthProperty, 10.0);
factory.SetValue(FrameworkElement.HeightProperty, 12.0);
factory.SetBinding(Shape.FillProperty, new Binding(Control.ForegroundProperty.Name)
{
RelativeSource = new RelativeSource
{
AncestorType = typeof(ComboBoxItem)
}
});
switch (verticalAlignment)
{
case VerticalAlignment.Top:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResource<Geometry>("AlignTopGeometry"));
break;
case VerticalAlignment.Center:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResource<Geometry>("AlignVCenterGeometry"));
break;
case VerticalAlignment.Bottom:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResource<Geometry>("AlignBottomGeometry"));
break;
case VerticalAlignment.Stretch:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResource<Geometry>("AlignVStretchGeometry"));
break;
}
dataTemplate.VisualTree = factory;
return dataTemplate;
}
return null;
}
}
}
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using HandyControl.Properties.Langs;
namespace HandyControl.Controls
@@ -22,18 +23,20 @@ namespace HandyControl.Controls
[typeof(float)] = EditorTypeCode.SingleNumber,
[typeof(double)] = EditorTypeCode.DoubleNumber,
[typeof(bool)] = EditorTypeCode.Switch,
[typeof(DateTime)] = EditorTypeCode.DateTime
[typeof(DateTime)] = EditorTypeCode.DateTime,
[typeof(HorizontalAlignment)] = EditorTypeCode.HorizontalAlignment,
[typeof(VerticalAlignment)] = EditorTypeCode.VerticalAlignment
};
public string ResolveCategory(PropertyDescriptor propertyDescriptor)
{
var category = propertyDescriptor.Category;
if (string.IsNullOrEmpty(category))
{
category = Lang.Miscellaneous;
}
var categoryAttribute = propertyDescriptor.Attributes.OfType<CategoryAttribute>().FirstOrDefault();
return category;
return categoryAttribute == null ?
Lang.Miscellaneous :
string.IsNullOrEmpty(categoryAttribute.Category) ?
Lang.Miscellaneous :
categoryAttribute.Category;
}
public string ResolveDisplayName(PropertyDescriptor propertyDescriptor)
@@ -88,6 +91,8 @@ namespace HandyControl.Controls
EditorTypeCode.DoubleNumber => new NumberPropertyEditor(double.MinValue, double.MaxValue),
EditorTypeCode.Switch => new SwitchPropertyEditor(),
EditorTypeCode.DateTime => new DateTimePropertyEditor(),
EditorTypeCode.HorizontalAlignment => new HorizontalAlignmentPropertyEditor(),
EditorTypeCode.VerticalAlignment => new VerticalAlignmentPropertyEditor(),
_ => new ReadOnlyTextPropertyEditor()
}
: type.IsSubclassOf(typeof(Enum))
@@ -110,7 +115,9 @@ namespace HandyControl.Controls
SingleNumber,
DoubleNumber,
Switch,
DateTime
DateTime,
HorizontalAlignment,
VerticalAlignment
}
}
}
@@ -1,5 +1,4 @@
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Tools;
@@ -9,11 +8,6 @@ namespace HandyControl.Controls
{
public class BlurWindow : Window
{
static BlurWindow()
{
StyleProperty.OverrideMetadata(typeof(BlurWindow), new FrameworkPropertyMetadata(ResourceHelper.GetResource<Style>(ResourceToken.WindowBlur)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
@@ -48,12 +48,6 @@ namespace HandyControl.Controls
set => SetValue(InactiveGlowColorProperty, value);
}
static GlowWindow()
{
StyleProperty.OverrideMetadata(typeof(GlowWindow), new FrameworkPropertyMetadata(ResourceHelper.GetResource<Style>(ResourceToken.WindowGlow)));
ResizeModeProperty.OverrideMetadata(typeof(GlowWindow), new FrameworkPropertyMetadata(OnResizeModeChanged));
}
#region internal
internal void EndDeferGlowChanges()
@@ -358,6 +358,18 @@
internal const string TabItemCapsuleVerticalLast = nameof(TabItemCapsuleVerticalLast);
internal const string ComboBoxItemCapsuleDefault = nameof(ComboBoxItemCapsuleDefault);
internal const string ComboBoxItemCapsuleSingle = nameof(ComboBoxItemCapsuleSingle);
internal const string ComboBoxItemCapsuleHorizontalFirst = nameof(ComboBoxItemCapsuleHorizontalFirst);
internal const string ComboBoxItemCapsuleHorizontalLast = nameof(ComboBoxItemCapsuleHorizontalLast);
internal const string ComboBoxItemCapsuleVerticalFirst = nameof(ComboBoxItemCapsuleVerticalFirst);
internal const string ComboBoxItemCapsuleVerticalLast = nameof(ComboBoxItemCapsuleVerticalLast);
#endregion
}
}
@@ -35,11 +35,13 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Divider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Drawer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Magnifier.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\ButtonGroup.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Button\ButtonGroup.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Card.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Notification.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Poptip.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Watermark.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\PropertyGrid\Editors\VerticalAlignmentPropertyEditor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\PropertyGrid\Editors\HorizontalAlignmentPropertyEditor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\PropertyGrid\Editors\EnumPropertyEditor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\PropertyGrid\Editors\ReadOnlyTextPropertyEditor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\PropertyGrid\Editors\TimePropertyEditor.cs" />
@@ -370,6 +372,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Tools\RegularJudgment.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\RegularPatterns.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\StyleSelector\ButtonGroupItemStyleSelector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\StyleSelector\ComboBoxItemCapsuleStyleSelector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\StyleSelector\TabItemCapsuleStyleSelector.cs" />
</ItemGroup>
<ItemGroup>
@@ -26,6 +26,14 @@
<Geometry o:Freeze="True" x:Key="WindowRestoreGeometry">M1,3 L1,9 L7,9 L7,3 z M3,1 L3,2 L8,2 L8,7 L9,7 L9,1 z M2,0 L10,0 L10,8 L8,8 L8,10 L0,10 L0,2 L2,2 z</Geometry>
<Geometry o:Freeze="True" x:Key="WindowMaxGeometry">M1,1 L1,9 L9,9 L9,1 z M0,0 L10,0 L10,10 L0,10 z</Geometry>
<Geometry o:Freeze="True" x:Key="CheckedGeometry">M 0,2 3,5 8,0</Geometry>
<Geometry o:Freeze="True" x:Key="AlignLeftGeometry">M0,8 L4,8 4,10 0,10 z M0,4 L8,4 8,6 0,6 z M0,0 L12,0 12,2 0,2 z</Geometry>
<Geometry o:Freeze="True" x:Key="AlignRightGeometry">M8,8 L12,8 12,10 8,10 z M4,4 L12,4 12,6 4,6 z M0,0 L12,0 12,2 0,2 z</Geometry>
<Geometry o:Freeze="True" x:Key="AlignHCenterGeometry">M4,8 L8,8 8,10 4,10 z M2,4 L10,4 10,6 2,6 z M0,0 L12,0 12,2 0,2 z</Geometry>
<Geometry o:Freeze="True" x:Key="AlignHStretchGeometry">M0,8 L12,8 12,10 0,10 z M0,4 L12,4 12,6 0,6 z M0,0 L12,0 12,2 0,2 z</Geometry>
<Geometry o:Freeze="True" x:Key="AlignTopGeometry">M8,0 L10,0 10,4 8,4 z M4,0 L6,0 6,8 4,8 z M0,0 L2,0 2,12 0,12 z</Geometry>
<Geometry o:Freeze="True" x:Key="AlignBottomGeometry">M8,8 L10,8 10,12 8,12 z M4,4 L6,4 6,12 4,12 z M0,0 L2,0 2,12 0,12 z</Geometry>
<Geometry o:Freeze="True" x:Key="AlignVCenterGeometry">M8,4 L10,4 10,8 8,8 z M4,2 L6,2 6,10 4,10 z M0,0 L2,0 2,12 0,12 z</Geometry>
<Geometry o:Freeze="True" x:Key="AlignVStretchGeometry">M8,0 L10,0 10,12 8,12 z M4,0 L6,0 6,12 4,12 z M0,0 L2,0 2,12 0,12 z</Geometry>
<Geometry o:Freeze="True" x:Key="PageModeGeometry">M1.0000002,12 L1.0000002,13 11,13 11,12 z M1.0000002,10 L1.0000002,11 11,11 11,10 z M1.0000002,8 L1.0000002,9 11,9 11,8 z M1.0000002,6 L1.0000002,7 11,7 11,6 z M1.0000002,4 L1.0000002,5 11,5 11,4 z M1.0000002,2 L1.0000002,3 11,3 11,2 z M0,0 L12,0 12,14 0,14 z</Geometry>
<Geometry o:Freeze="True" x:Key="TwoPageModeGeometry">M8.99999988079071,12L8.99999988079071,13 13,13 13,12z M1,12L1,13 5,13 5,12z M9.00000011920929,10L9.00000011920929,11 13,11 13,10z M1,10L1,11 5,11 5,10z M9.00000011920929,8L9.00000011920929,9 13,9 13,8z M1,8L1,9 5,9 5,8z M9.00000011920929,6L9.00000011920929,7 13,7 13,6z M1,6L1,7 5,7 5,6z M9.00000011920929,4L9.00000011920929,5 13,5 13,4z M1,4L1,5 5,5 5,4z M8.99999988079071,2L8.99999988079071,3 13,3 13,2z M1,2L1,3 5,3 5,2z M7.99999994039536,0L14,0 14,14 7.99999994039536,14z M0,0L6,0 6,14 0,14z</Geometry>
<Geometry o:Freeze="True" x:Key="ScrollModeGeometry">M1,12L1,13 8,13 8,12z M1,10L1,11 8,11 8,10z M1,8L1,9 8,9 8,8z M1,6L1,7 8,7 8,6z M11.0000002384186,5L11.0000002384186,9 13,9 13,5z M1,4L1,5 8,5 8,4z M1,2L1,3 8,3 8,2z M10.0000000298023,0L14,0 14,14 10.0000000298023,14z M0,0L9.00000011920929,0 9.00000011920929,14 0,14z</Geometry>
@@ -1,6 +1,7 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:HandyControl.Controls">
xmlns:controls="clr-namespace:HandyControl.Controls"
xmlns:tools="clr-namespace:HandyControl.Tools">
<ItemsPanelTemplate x:Key="ButtonGroupHorizontalItemsPanelTemplate">
<UniformGrid FocusVisualStyle="{x:Null}" Rows="1"/>
@@ -10,7 +11,10 @@
<UniformGrid FocusVisualStyle="{x:Null}" Columns="1"/>
</ItemsPanelTemplate>
<tools:ButtonGroupItemStyleSelector x:Key="ButtonGroupItemStyleSelector"/>
<Style x:Key="ButtonGroupBaseStyle" TargetType="controls:ButtonGroup">
<Setter Property="ItemContainerStyleSelector" Value="{StaticResource ButtonGroupItemStyleSelector}"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Focusable" Value="False"/>
@@ -115,7 +115,7 @@
<Setter Property="Focusable" Value="False"/>
</Style>
<Style TargetType="hc:PropertyGrid">
<Style x:Key="PropertyGridBaseStyle" TargetType="hc:PropertyGrid">
<Setter Property="MaxTitleWidth" Value="200"/>
<Setter Property="MinTitleWidth" Value="120"/>
<Setter Property="Focusable" Value="False"/>
@@ -1,11 +1,100 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:controls="clr-namespace:HandyControl.Controls"
xmlns:themes="clr-namespace:HandyControl.Themes">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:themes="clr-namespace:HandyControl.Themes"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
xmlns:tools="clr-namespace:HandyControl.Tools">
<ResourceDictionary.MergedDictionaries>
<themes:SharedResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Styles/Base/PropertyGridBaseStyle.xaml"/>
<themes:SharedResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Styles/ComboBox.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style BasedOn="{StaticResource PropertyGridBaseStyle}" TargetType="controls:Badge"/>
<Style BasedOn="{StaticResource PropertyGridBaseStyle}" TargetType="controls:PropertyGrid"/>
<Style x:Key="ComboBoxItemCapsuleBaseStyle" BasedOn="{x:Null}" TargetType="ComboBoxItem">
<Setter Property="MinWidth" Value="40"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Background" Value="{DynamicResource RegionBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="controls:BorderElement.CornerRadius" Value="0"/>
<Setter Property="MinHeight" Value="{StaticResource DefaultControlHeight}"/>
<Setter Property="Padding" Value="10,0"/>
<Setter Property="Margin" Value="-1,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Border x:Name="BorderRoot" CornerRadius="{Binding Path=(controls:BorderElement.CornerRadius),RelativeSource={RelativeSource TemplatedParent}}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter x:Name="contentPresenter" ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}" TextElement.Foreground="{TemplateBinding Foreground}" Focusable="False" HorizontalAlignment="Center" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Panel.ZIndex" Value="{x:Static system:Int32.MaxValue}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource SecondaryRegionBrush}"/>
<Setter Property="TextElement.Foreground" Value="{DynamicResource PrimaryBrush}"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="BorderBrush" Value="{DynamicResource PrimaryBrush}"/>
<Setter Property="Background" Value="{DynamicResource PrimaryBrush}"/>
<Setter Property="TextElement.Foreground" Value="{DynamicResource TextIconBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="ComboBoxItemCapsuleDefault" BasedOn="{StaticResource ComboBoxItemCapsuleBaseStyle}" TargetType="ComboBoxItem"/>
<Style x:Key="ComboBoxItemCapsuleHorizontalFirst" BasedOn="{StaticResource ComboBoxItemCapsuleBaseStyle}" TargetType="ComboBoxItem">
<Setter Property="controls:BorderElement.CornerRadius" Value="4,0,0,4"/>
<Setter Property="Margin" Value="0"/>
</Style>
<Style x:Key="ComboBoxItemCapsuleHorizontalLast" BasedOn="{StaticResource ComboBoxItemCapsuleBaseStyle}" TargetType="ComboBoxItem">
<Setter Property="controls:BorderElement.CornerRadius" Value="0,4,4,0"/>
</Style>
<Style x:Key="ComboBoxItemCapsuleSingle" BasedOn="{StaticResource ComboBoxItemCapsuleBaseStyle}" TargetType="ComboBoxItem">
<Setter Property="controls:BorderElement.CornerRadius" Value="{StaticResource DefaultCornerRadius}"/>
</Style>
<Style x:Key="ComboBoxItemCapsuleVerticalFirst" BasedOn="{StaticResource ComboBoxItemCapsuleBaseStyle}" TargetType="ComboBoxItem">
<Setter Property="controls:BorderElement.CornerRadius" Value="4,4,0,0"/>
<Setter Property="Margin" Value="0"/>
</Style>
<Style x:Key="ComboBoxItemCapsuleVerticalLast" BasedOn="{StaticResource ComboBoxItemCapsuleBaseStyle}" TargetType="ComboBoxItem">
<Setter Property="controls:BorderElement.CornerRadius" Value="0,0,4,4"/>
</Style>
<ItemsPanelTemplate x:Key="ComboBoxCapsuleItemsPanelTemplate">
<UniformGrid FocusVisualStyle="{x:Null}" Rows="1"/>
</ItemsPanelTemplate>
<tools:ComboBoxItemCapsuleStyleSelector x:Key="ComboBoxItemCapsuleStyleSelector"/>
<Style x:Key="ComboBoxCapsule" BasedOn="{x:Null}" TargetType="ComboBox">
<Setter Property="ItemContainerStyleSelector" Value="{StaticResource ComboBoxItemCapsuleStyleSelector}"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="ItemsPanel" Value="{StaticResource ComboBoxCapsuleItemsPanelTemplate}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<controls:HorizontalAlignmentPathTemplateSelector x:Key="HorizontalAlignmentPathTemplateSelector"/>
<controls:VerticalAlignmentPathTemplateSelector x:Key="VerticalAlignmentPathTemplateSelector"/>
</ResourceDictionary>
@@ -0,0 +1,30 @@
using System.Windows;
using System.Windows.Controls;
using HandyControl.Data;
namespace HandyControl.Tools
{
public class ComboBoxItemCapsuleStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
if (container is ComboBoxItem comboBoxItem && VisualHelper.GetParent<ComboBox>(comboBoxItem) is { } comboBox)
{
var count = comboBox.Items.Count;
if (count == 1)
{
return ResourceHelper.GetResource<Style>(ResourceToken.ComboBoxItemCapsuleSingle);
}
var index = comboBox.ItemContainerGenerator.IndexFromContainer(comboBoxItem);
return index == 0
? ResourceHelper.GetResource<Style>(ResourceToken.ComboBoxItemCapsuleHorizontalFirst)
: ResourceHelper.GetResource<Style>(index == count - 1
? ResourceToken.ComboBoxItemCapsuleHorizontalLast
: ResourceToken.ComboBoxItemCapsuleDefault);
}
return null;
}
}
}
@@ -8,7 +8,7 @@ namespace HandyControl.Tools
{
public override Style SelectStyle(object item, DependencyObject container)
{
if (item is TabItem tabItem && tabItem.Parent is TabControl tabControl)
if (container is TabItem tabItem && VisualHelper.GetParent<TabControl>(tabItem) is { } tabControl)
{
var count = tabControl.Items.Count;
if (count == 1)
@@ -16,7 +16,7 @@ namespace HandyControl.Tools
return ResourceHelper.GetResource<Style>(ResourceToken.TabItemCapsuleSingle);
}
var index = tabControl.Items.IndexOf(tabItem);
var index = tabControl.ItemContainerGenerator.IndexFromContainer(tabItem);
return index == 0
? ResourceHelper.GetResource<Style>(
tabControl.TabStripPlacement == Dock.Top || tabControl.TabStripPlacement == Dock.Bottom