Files
ShrlAlgoToolkit/NeuWPF/NeoUI/Controls/Cascader.xaml
ShrlAlgo 955a01f564 整理
2025-08-20 12:10:35 +08:00

450 lines
30 KiB
XML

<!-- Cascader.Resources.xaml -->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:assists="clr-namespace:NeoUI.Assists"
xmlns:conv="clr-namespace:NeoUI.Converters"
xmlns:internal="clr-namespace:NeoUI.Converters.Internal"
xmlns:local="clr-namespace:NeoUI.Controls"
xmlns:svd="clr-namespace:NeoUI.Controls.Decorations"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--<ResourceDictionary.MergedDictionaries>
-->
<!-- 引入其他资源字典 -->
<!--
<ResourceDictionary Source="pack://application:,,,/NeuWPF;component/Controls/ListBoxStyle.xaml" />
</ResourceDictionary.MergedDictionaries>-->
<!-- 2. 修正 ListBox 的样式 -->
<!-- 按钮高亮 -->
<Storyboard x:Key="Button.FocusEffect">
<DoubleAnimation
Duration="0:0:0.2"
Storyboard.TargetName="border"
Storyboard.TargetProperty="Intensity"
To="0.8" />
<BooleanAnimationUsingKeyFrames
Duration="0"
Storyboard.TargetName="border"
Storyboard.TargetProperty="ShaderEnabled">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
<!-- 按钮取消高亮 -->
<Storyboard x:Key="Button.LeaveEffect">
<DoubleAnimation
Duration="0:0:0.2"
Storyboard.TargetName="border"
Storyboard.TargetProperty="Intensity"
To="0" />
<BooleanAnimationUsingKeyFrames
Duration="0:0:0.2"
Storyboard.TargetName="border"
Storyboard.TargetProperty="ShaderEnabled">
<DiscreteBooleanKeyFrame KeyTime="0:0:0.2" Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
<!-- 按钮按下 -->
<Storyboard x:Key="Button.PressEffect">
<DoubleAnimation
Duration="0"
Storyboard.TargetName="border"
Storyboard.TargetProperty="Intensity"
To="0.4" />
</Storyboard>
<!--<Style x:Key="DefaultCascaderListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Padding" Value="4,2" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource FocusVisual}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border
x:Name="Bd"
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="4"
SnapsToDevicePixels="true">
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Bd" Property="Background" Value="{DynamicResource ControlBackgroundSelectedBrush}" />
<Setter Property="Foreground" Value="{DynamicResource TextOnAccentPrimaryBrush}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Bd" Property="Background" Value="{DynamicResource ControlBackgroundHoverBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource TextDisabledBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>-->
<Style TargetType="ListBox" x:Key="DefaultCascaderPanelStyle">
<!--<Setter Property="ItemContainerStyle" Value="{DynamicResource DefaultCascaderListBoxItemStyle}" />-->
<Setter Property="Background" Value="{DynamicResource BackgroundFloatingBrush}" />
<Setter Property="BorderBrush" Value="{DynamicResource BorderNormalBrush}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Foreground" Value="{DynamicResource TextPrimaryBrush}" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Padding" Value="10,4" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="AlternationCount" Value="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count}" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!-- 核心修正:使用 TextBlock 并通过 MultiBinding 设置 Text -->
<TextBlock Grid.Column="0" VerticalAlignment="Center">
<TextBlock.Text>
<MultiBinding Converter="{x:Static internal:PropertyValueConverter.Instance}">
<!-- 第一个绑定:当前的数据项 -->
<Binding Path="." />
<!-- 第二个绑定:从祖先 Cascader 获取 DisplayMemberPath -->
<Binding Path="DisplayMemberPath" RelativeSource="{RelativeSource AncestorType=local:Cascader}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<Path
Data="M 0,0 L 4,4 L 0,8 Z"
Fill="{DynamicResource TextPrimaryBrush}"
Grid.Column="1"
Height="8"
HorizontalAlignment="Right"
Stretch="Uniform"
VerticalAlignment="Center"
Visibility="Hidden"
Width="8"
x:Name="Arrow" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{x:Static internal:HasChildrenMultiConverter.Instance}">
<Binding Path="." />
<Binding Path="SubmenuMemberPath" RelativeSource="{RelativeSource AncestorType=local:Cascader}" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Visibility" TargetName="Arrow" Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="4"
SnapsToDevicePixels="true"
x:Name="Bd">
<ScrollViewer Focusable="false">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource ControlBackgroundDisabledBrush}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsGrouping" Value="true" />
<Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="ScrollViewer.CanContentScroll" Value="false" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type local:Cascader}">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Background" Value="{DynamicResource ControlBackgroundNormalBrush}" />
<Setter Property="BorderBrush" Value="{DynamicResource BorderNormalBrush}" />
<Setter Property="Foreground" Value="{DynamicResource TextPrimaryBrush}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Padding" Value="10,8" />
<Setter Property="Margin" Value="4" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="assists:InputAssist.Clearable" Value="True" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.CanContentScroll" Value="True" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="PanelStyle" Value="{StaticResource DefaultCascaderPanelStyle}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Cascader}">
<!-- ... Grid 和 ToggleButton 保持不变 ... -->
<Grid SnapsToDevicePixels="true" x:Name="templateRoot">
<ToggleButton
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
IsChecked="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
x:Name="PART_ToggleButton">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Focusable" Value="False" />
<Setter Property="ClickMode" Value="Press" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<svd:EmbossBorder
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="4"
Intensity="0"
Margin="-4"
ShaderEnabled="False"
SnapsToDevicePixels="True"
x:Name="border">
<Grid Margin="0">
<TextBlock
HorizontalAlignment="Left"
IsHitTestVisible="False"
Text="{Binding SelectedText, RelativeSource={RelativeSource AncestorType=local:Cascader}}"
TextTrimming="CharacterEllipsis"
VerticalAlignment="Center" />
<Border
BorderBrush="Transparent"
BorderThickness="1"
HorizontalAlignment="Right"
Margin="0"
SnapsToDevicePixels="True"
Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"
x:Name="splitBorder">
<Path
Data="M 0,0 L 4,3 L 8,0"
HorizontalAlignment="Center"
Margin="0"
Stroke="{DynamicResource TextPrimaryBrush}"
StrokeThickness="1"
VerticalAlignment="Center"
x:Name="arrow" />
</Border>
</Grid>
<!--<Grid>
<TextBlock
Margin="{TemplateBinding Padding}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
IsHitTestVisible="False"
Text="{Binding SelectedText, RelativeSource={RelativeSource AncestorType=local:Cascader}}"
TextTrimming="CharacterEllipsis" />
<Path
x:Name="arrow"
Margin="0,0,8,0"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"
Fill="#555555"
IsHitTestVisible="False" />
</Grid>-->
</svd:EmbossBorder>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource BorderGradientBrush}" />
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Button.FocusEffect}" x:Name="showEffectBegin" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource Button.LeaveEffect}" />
</Trigger.ExitActions>
</Trigger>
<!-- 缩减阴影,下压效果 -->
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Button.PressEffect}" x:Name="pressBegin" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="pressBegin" />
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource BorderGradientBrush}" />
</Trigger>
<!-- 按钮禁用效果 -->
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Fill" TargetName="arrow" Value="{DynamicResource TextDisabledBrush}" />
<Setter Property="Background" TargetName="border" Value="{DynamicResource ControlBackgroundDisabledBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
<TextBlock
Foreground="{DynamicResource TextSecondaryBrush}"
IsHitTestVisible="False"
Margin="{TemplateBinding Padding}"
Text="{Binding Path=(assists:SelectorAssist.Placeholder), RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:Cascader}}"
VerticalAlignment="Center"
x:Name="Placeholder">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedText, RelativeSource={RelativeSource AncestorType=local:Cascader}}" Value="">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<Popup
AllowsTransparency="True"
IsOpen="{Binding IsChecked, ElementName=PART_ToggleButton}"
Margin="-8"
Placement="Bottom"
PlacementTarget="{Binding ElementName=PART_ToggleButton}"
PopupAnimation="Slide"
StaysOpen="False"
x:Name="PART_Popup">
<Border
Background="{DynamicResource BackgroundLayoutBrush}"
BorderBrush="{DynamicResource BorderNormalBrush}"
BorderThickness="1"
CornerRadius="4"
Effect="{DynamicResource PopupShadow}"
Margin="8"
MaxHeight="300"
MinWidth="{Binding ActualWidth, ElementName=templateRoot}"
SnapsToDevicePixels="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox
Grid.Row="0"
Margin="4"
Text="{Binding SearchText, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"
x:Name="PART_SearchBox">
<assists:InputAssist.Suffix>
<Path
Data="M 6.332031 2 C 8.726562 2 10.667969 3.941406 10.667969 6.332031 C 10.667969 7.40625 10.273438 8.394531 9.625 9.152344 L 9.808594 9.332031 L 10.332031 9.332031 L 13.667969 12.667969 L 12.667969 13.667969 L 9.332031 10.332031 L 9.332031 9.808594 L 9.152344 9.625 C 8.394531 10.273438 7.40625 10.667969 6.332031 10.667969 C 3.941406 10.667969 2 8.726562 2 6.332031 C 2 3.941406 3.941406 2 6.332031 2 M 6.332031 3.332031 C 4.667969 3.332031 3.332031 4.667969 3.332031 6.332031 C 3.332031 8 4.667969 9.332031 6.332031 9.332031 C 8 9.332031 9.332031 8 9.332031 6.332031 C 9.332031 4.667969 8 3.332031 6.332031 3.332031 Z M 6.332031 3.332031 "
DockPanel.Dock="Left"
Fill="{DynamicResource TextSecondaryBrush}"
Height="16"
Margin="4,2,2,0"
VerticalAlignment="Center"
Width="16" />
</assists:InputAssist.Suffix>
</TextBox>
<Grid Grid.Row="1">
<ScrollViewer
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Disabled"
x:Name="PART_CascadingView">
<ContentPresenter Content="{TemplateBinding CascadingPanel}" />
</ScrollViewer>
<!-- 改造搜索结果列表 -->
<ListBox
HorizontalContentAlignment="Stretch"
ItemsSource="{Binding FilteredItemsSource, RelativeSource={RelativeSource TemplatedParent}}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Visibility="Collapsed"
x:Name="PART_SearchResultsView">
<ListBox.ItemTemplate>
<DataTemplate>
<!-- 使用一个完全透明的按钮来承载内容和命令 -->
<Button
Command="{Binding RelativeSource={RelativeSource AncestorType=local:Cascader}, Path=SelectSearchResultCommand}"
CommandParameter="{Binding}"
HorizontalContentAlignment="Stretch"
Padding="4,2">
<Button.Template>
<ControlTemplate TargetType="Button">
<Border
Background="Transparent"
Padding="{TemplateBinding Padding}"
x:Name="Bd">
<ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource ControlBackgroundHoverBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
<!-- 按钮的内容就是我们之前定义的文本 -->
<TextBlock
Text="{Binding DisplayText}"
TextTrimming="CharacterEllipsis"
ToolTip="{Binding DisplayText}" />
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</Border>
</Popup>
</Grid>
<!-- ... ControlTemplate.Triggers 保持不变 ... -->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="#7EB4EA" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="#FFF4F4F4" />
<Setter Property="Foreground" Value="#FF888888" />
</Trigger>
<Trigger Property="IsDropDownOpen" Value="True">
<Setter Property="BorderBrush" Value="#569DE5" />
</Trigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=SearchText, RelativeSource={RelativeSource Self}, Converter={x:Static conv:NullOrEmptyConverter.NotInstance}}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" TargetName="PART_CascadingView" Value="Collapsed" />
<Setter Property="Visibility" TargetName="PART_SearchResultsView" Value="Visible" />
</MultiDataTrigger>
<Trigger Property="IsSearchable" Value="False">
<Setter Property="Visibility" TargetName="PART_SearchBox" Value="Collapsed" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>