Files
Shrlalgo.RvKits/WPFluent/Controls/ComboBox/ComboBox.xaml
2025-05-05 17:04:06 +08:00

378 lines
24 KiB
XML

<!--
This Source Code Form is subject to the terms of the MIT License.
If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
Copyright (C) Leszek Pomianowski and WPF UI Contributors.
All Rights Reserved.
Based on Microsoft XAML for Win UI
Copyright (c) Microsoft Corporation. All Rights Reserved.
-->
<!-- TODO: Refactor editable and fix borders -->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:WPFluent.Controls"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WPFluent;component/Resources/Variables.xaml" />
</ResourceDictionary.MergedDictionaries>
<Thickness x:Key="ComboBoxPadding">10,8,10,8</Thickness>
<Thickness x:Key="ComboBoxBorderThemeThickness">1,1,1,1</Thickness>
<Thickness x:Key="ComboBoxAccentBorderThemeThickness">0,0,0,2</Thickness>
<Thickness x:Key="ComboBoxChevronMargin">8,0,10,0</Thickness>
<Thickness x:Key="ComboBoxItemMargin">6,4,6,0</Thickness>
<Thickness x:Key="ComboBoxItemContentMargin">10,8,8,8</Thickness>
<system:Double x:Key="ComboBoxChevronSize">11.0</system:Double>
<system:Double x:Key="ComboBoxPopupMinHeight">32.0</system:Double>
<Style x:Key="DefaultComboBoxTextBoxStyle" TargetType="{x:Type TextBox}">
<!-- Focus by parent element -->
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<!-- Focus by parent element -->
<!-- Universal WPF UI ContextMenu -->
<Setter Property="ContextMenu" Value="{DynamicResource DefaultControlContextMenu}" />
<!-- Universal WPF UI ContextMenu -->
<Setter Property="Background" Value="Transparent" />
<Setter Property="CaretBrush" Value="{DynamicResource ComboBoxForeground}" />
<Setter Property="Cursor" Value="IBeam" />
<Setter Property="Foreground" Value="{DynamicResource ComboBoxForeground}" />
<Setter Property="Margin" Value="0" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Padding" Value="0" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<controls:PassiveScrollViewer
x:Name="PART_ContentHost"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Style="{DynamicResource DefaultTextBoxScrollViewerStyle}"
TextElement.Foreground="{TemplateBinding Foreground}" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Visibility" Value="Hidden" />
</Style>
<Style x:Key="DefaultComboBoxToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<!-- Focus by parent element -->
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<!-- Focus by parent element -->
<Setter Property="Background" Value="Transparent" />
<Setter Property="Border.CornerRadius" Value="0" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="{DynamicResource ComboBoxForeground}" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border
x:Name="ContentBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding Border.CornerRadius}">
<ContentPresenter
x:Name="PART_ContentHost"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="DefaultComboBoxItemStyle" TargetType="{x:Type ComboBoxItem}">
<!-- Universal WPF UI focus -->
<Setter Property="FocusVisualStyle" Value="{DynamicResource DefaultControlFocusVisualStyle}" />
<!-- Universal WPF UI focus -->
<Setter Property="Background" Value="Transparent" />
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Foreground" Value="{DynamicResource ComboBoxForeground}" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Padding" Value="{StaticResource ComboBoxItemContentMargin}" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Grid Background="Transparent">
<Border
Margin="{DynamicResource ComboBoxItemMargin}"
VerticalAlignment="Stretch"
CornerRadius="{TemplateBinding Border.CornerRadius}"
Name="ContentBorder"
Padding="0"
SnapsToDevicePixels="True">
<Grid>
<ContentPresenter
x:Name="PART_ContentPresenter"
Margin="{TemplateBinding Padding}"
VerticalAlignment="Center" />
<Rectangle
x:Name="ActiveRectangle"
Width="3"
Height="16"
Margin="0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Fill="{DynamicResource ComboBoxItemPillFillBrush}"
RadiusX="2"
RadiusY="2"
Visibility="Collapsed" />
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource ComboBoxItemBackgroundSelected}" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="PART_ContentPresenter" Property="TextElement.Foreground" Value="{DynamicResource ComboBoxItemForegroundSelected}" />
<Setter TargetName="ActiveRectangle" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundDisabled}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="DefaultComboBoxStyle" TargetType="{x:Type ComboBox}">
<!-- Universal WPF UI focus -->
<Setter Property="FocusVisualStyle" Value="{DynamicResource DefaultControlFocusVisualStyle}" />
<!-- Universal WPF UI focus -->
<!-- Universal WPF UI ContextMenu -->
<Setter Property="ContextMenu" Value="{DynamicResource DefaultControlContextMenu}" />
<!-- Universal WPF UI ContextMenu -->
<Setter Property="Background" Value="{DynamicResource ComboBoxBackground}" />
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="BorderBrush" Value="{DynamicResource ControlElevationBorderBrush}" />
<Setter Property="BorderThickness" Value="{StaticResource ComboBoxBorderThemeThickness}" />
<Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}" />
<Setter Property="Foreground" Value="{DynamicResource ComboBoxForeground}" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="MinHeight" Value="{StaticResource TextControlThemeMinHeight}" />
<Setter Property="MinWidth" Value="{StaticResource TextControlThemeMinWidth}" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Padding" Value="{StaticResource ComboBoxPadding}" />
<Setter Property="Popup.Placement" Value="Bottom" />
<Setter Property="Popup.PopupAnimation" Value="None" />
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
<Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
<Setter Property="VerticalAlignment" Value="Center" />
<!-- WPF doesn't like centering, the animation is ugly and the mouse button sometimes clicks right away. -->
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid>
<Border
x:Name="AccentBorder"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderBrush="{DynamicResource ComboBoxBorderBrushFocused}"
BorderThickness="{StaticResource ComboBoxAccentBorderThemeThickness}"
CornerRadius="{TemplateBinding Border.CornerRadius}"
Visibility="Collapsed" />
<Border
x:Name="ContentBorder"
Grid.Row="0"
MinWidth="{TemplateBinding MinWidth}"
MinHeight="{TemplateBinding MinHeight}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding Border.CornerRadius}">
<Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Popup
x:Name="Popup"
VerticalAlignment="Center"
AllowsTransparency="True"
Focusable="False"
IsOpen="{TemplateBinding IsDropDownOpen}"
Placement="{TemplateBinding Popup.Placement}"
PopupAnimation="{TemplateBinding Popup.PopupAnimation}"
VerticalOffset="1">
<Border
x:Name="DropDownBorder"
MinWidth="{TemplateBinding ActualWidth}"
Margin="0"
Background="{DynamicResource ComboBoxDropDownBackground}"
BorderBrush="{DynamicResource ComboBoxDropDownBorderBrush}"
BorderThickness="1"
CornerRadius="{DynamicResource PopupCornerRadius}"
Padding="0,4,0,6"
SnapsToDevicePixels="True">
<Border.RenderTransform>
<TranslateTransform />
</Border.RenderTransform>
<Grid>
<controls:DynamicScrollViewer
MaxHeight="{TemplateBinding MaxDropDownHeight}"
Margin="0"
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
SnapsToDevicePixels="True"
TextElement.FontSize="{TemplateBinding FontSize}"
TextElement.FontWeight="{TemplateBinding FontWeight}"
TextElement.Foreground="{TemplateBinding Foreground}"
VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
<StackPanel
IsItemsHost="True"
KeyboardNavigation.DirectionalNavigation="Contained"
TextElement.FontSize="{TemplateBinding FontSize}" />
</controls:DynamicScrollViewer>
</Grid>
</Border>
</Popup>
<ContentPresenter
Grid.Column="0"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
IsHitTestVisible="False"
Name="PART_ContentPresenter"
TextElement.Foreground="{TemplateBinding Foreground}" />
<ToggleButton
Grid.Column="0"
Grid.ColumnSpan="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ClickMode="Press"
Focusable="False"
Foreground="{TemplateBinding Foreground}"
IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
Name="ToggleButton"
Style="{StaticResource DefaultComboBoxToggleButtonStyle}" />
<TextBox
x:Name="PART_EditableTextBox"
Grid.Column="0"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}"
IsReadOnly="{TemplateBinding IsReadOnly}"
Style="{StaticResource DefaultComboBoxTextBoxStyle}" />
<Grid Grid.Column="1" Margin="{StaticResource ComboBoxChevronMargin}">
<controls:SymbolIcon
x:Name="ChevronIcon"
VerticalAlignment="Center"
FontSize="{StaticResource ComboBoxChevronSize}"
Foreground="{DynamicResource ComboBoxDropDownGlyphForeground}"
IsHitTestVisible="False"
RenderTransformOrigin="0.5, 0.5"
Symbol="ChevronDown24">
<controls:SymbolIcon.RenderTransform>
<RotateTransform Angle="0" />
</controls:SymbolIcon.RenderTransform>
</controls:SymbolIcon>
</Grid>
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsDropDownOpen" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Duration="00:00:00.167"
From="0"
Storyboard.TargetName="ChevronIcon"
Storyboard.TargetProperty="(controls:SymbolIcon.RenderTransform).(RotateTransform.Angle)"
To="180" />
<DoubleAnimation
Duration="00:00:00.167"
From="-90"
Storyboard.TargetName="DropDownBorder"
Storyboard.TargetProperty="(Border.RenderTransform).(TranslateTransform.Y)"
To="0">
<DoubleAnimation.EasingFunction>
<CircleEase EasingMode="EaseOut" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Duration="00:00:00.167"
From="180"
Storyboard.TargetName="ChevronIcon"
Storyboard.TargetProperty="(controls:SymbolIcon.RenderTransform).(RotateTransform.Angle)"
To="0" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="HasItems" Value="False">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="{StaticResource ComboBoxPopupMinHeight}" />
</Trigger>
<Trigger Property="Popup.AllowsTransparency" Value="False" SourceName="Popup">
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="0" />
</Trigger>
<Trigger Property="IsGrouping" Value="True">
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
</Trigger>
<Trigger Property="IsEditable" Value="True">
<Setter Property="IsTabStop" Value="False" />
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible" />
<Setter TargetName="PART_ContentPresenter" Property="Visibility" Value="Hidden" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsEnabled" Value="True" />
<Condition Property="IsKeyboardFocusWithin" Value="True" />
<Condition Property="IsEditable" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource ComboBoxBackgroundFocused}" />
<Setter TargetName="AccentBorder" Property="Visibility" Value="Visible" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsEnabled" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsKeyboardFocusWithin" Value="False" />
</MultiTrigger.Conditions>
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource ComboBoxBackgroundPointerOver}" />
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="ContentBorder" Property="Background" Value="{DynamicResource ComboBoxBackgroundDisabled}" />
<Setter TargetName="ContentBorder" Property="BorderBrush" Value="{DynamicResource ComboBoxBorderBrushDisabled}" />
<Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundDisabled}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
<Style BasedOn="{StaticResource DefaultComboBoxItemStyle}" TargetType="{x:Type ComboBoxItem}" />
<Style BasedOn="{StaticResource DefaultComboBoxStyle}" TargetType="{x:Type ComboBox}" />
</ResourceDictionary>