Kategorie

This commit is contained in:
Meik
2025-11-11 23:22:31 +01:00
parent 8cf1c84328
commit 5292a2cb0c
4 changed files with 175 additions and 159 deletions

View File

@@ -1,6 +1,7 @@
<UserControl x:Class="FasdDesktopUi.Basics.UserControls.HierarchicalSelectionControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FasdDesktopUi.Basics.UserControls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ico="clr-namespace:FasdDesktopUi.Basics.UserControls.AdaptableIcon;assembly=F4SD-AdaptableIcon"
@@ -15,146 +16,124 @@
<UserControl.Resources>
<vc:LanguageDefinitionsConverter x:Key="LanguageConverter" />
<vc:NullValueToVisibilityConverter x:Key="NullToVisibility" />
<ControlTemplate x:Key="HierarchicalComboBoxTemplate"
TargetType="{x:Type ComboBox}">
<Grid>
<ToggleButton x:Name="toggleButton"
Grid.ColumnSpan="2"
Focusable="False"
ClickMode="Press"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Style="{StaticResource ComboBoxToggleButton}" />
<ContentPresenter x:Name="contentPresenter"
IsHitTestVisible="False"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
Margin="6 0"
VerticalAlignment="Center"
HorizontalAlignment="Stretch" />
<Popup x:Name="PART_Popup"
Placement="Bottom"
PlacementTarget="{Binding ElementName=toggleButton}"
AllowsTransparency="True"
Focusable="False"
IsOpen="{TemplateBinding IsDropDownOpen}"
PopupAnimation="Fade">
<Border Width="{Binding ActualWidth, ElementName=HierarchySelector}"
Background="{DynamicResource BackgroundColor.Menu.SubCategory}"
CornerRadius="7.5"
Padding="10">
<Border.Effect>
<DropShadowEffect BlurRadius="10"
Color="{DynamicResource DropShadowColor.Menu}"
Direction="55"
ShadowDepth="10"
Opacity="0.35" />
</Border.Effect>
<StackPanel>
<Grid Margin="0 0 10 0">
<TextBox x:Name="PART_SearchTextBox"
Style="{DynamicResource Customizable.Editable.TextBox.EditOnly}"
Background="{TemplateBinding Background}"
Padding="27 5 5 5"
VerticalContentAlignment="Center" />
<ico:AdaptableIcon SelectedInternIcon="menuBar_search"
Style="{DynamicResource Menu.MenuBar.PinnedIcon.Base}"
BorderPadding="5 5 5 7"
Margin="0"
IconBackgroundColor="Transparent"
HorizontalAlignment="Left"
VerticalAlignment="Center" />
<TextBlock Margin="30 0 0 0"
Foreground="{DynamicResource FontColor.DetailsPage.DataHistory.Date}"
IsHitTestVisible="False"
VerticalAlignment="Center">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=PART_SearchTextBox}"
Value="">
<Setter Property="Text"
Value="{Binding Path=SearchPlaceholderText, RelativeSource={RelativeSource AncestorType={x:Type local:HierarchicalSelectionControl}}}" />
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
<Border Margin="0 10 0 0"
Background="{DynamicResource BackgroundColor.Menu.SubCategory}"
BorderThickness="0"
CornerRadius="5">
<ScrollViewer MaxHeight="300"
VerticalScrollBarVisibility="Auto">
<TreeView x:Name="PART_TreeView"
Background="{DynamicResource BackgroundColor.Menu.SubCategory}"
ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource TemplatedParent}}">
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded"
Value="{Binding IsExpanded, Mode=TwoWay}" />
</Style>
<HierarchicalDataTemplate DataType="{x:Type models:HierarchicalSelectionItem}"
ItemsSource="{Binding Children}">
<TextBlock Text="{Binding DisplayName}"
Foreground="{DynamicResource FontColor.Menu.Categories}"
Margin="0 2 0 2" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</ScrollViewer>
</Border>
</StackPanel>
</Border>
</Popup>
</Grid>
</ControlTemplate>
</UserControl.Resources>
<Grid>
<Border x:Name="DisplayBorder"
Height="28"
Background="{Binding ElementName=HierarchySelector, Path=ComboBoxBackground}"
BorderBrush="{Binding ElementName=HierarchySelector, Path=BorderBrush}"
BorderThickness="1"
CornerRadius="7.5"
Padding="6 0"
MouseLeftButtonUp="DisplayBorder_MouseLeftButtonUp">
<Border.Style>
<Style TargetType="Border">
<Setter Property="Opacity" Value="1" />
<Setter Property="Cursor" Value="Hand" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsOpen, ElementName=DropDownPopup}"
Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource BackgroundColor.Menu.SubCategory.Hover}" />
<Setter Property="Background" Value="{DynamicResource BackgroundColor.DetailsPage.DataHistory.ValueColumn}" />
</DataTrigger>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource BackgroundColor.Menu.SubCategory.Hover}" />
</Trigger>
<DataTrigger Binding="{Binding ElementName=HierarchySelector, Path=IsEnabled}"
Value="False">
<Setter Property="Opacity" Value="0.5" />
<Setter Property="Cursor" Value="Arrow" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0"
Margin="0 0 10 0"
VerticalAlignment="Center">
<TextBlock Text="{Binding ElementName=HierarchySelector, Path=SelectedItem.FullPath}"
Foreground="{DynamicResource FontColor.Menu.Categories}"
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"
ToolTip="{Binding ElementName=HierarchySelector, Path=SelectedItem.FullPath}" />
</Grid>
<Path Grid.Column="1"
Margin="0 0 6 0"
Data="F1 M 2.89067,4.99467C 2.89067,4.22933 3.724,3.74533 4.39067,4.13067L 10.8227,7.844L 17.26,4.13067C 18.412,3.48 19.4013,5.19333 18.26,5.86533L 11.3227,9.86533C 11.016,10.0413 10.636,10.0413 10.3227,9.86533L 3.39067,5.86533C 3.07867,5.688 2.89067,5.35467 2.89067,4.99467 Z"
Fill="{DynamicResource Color.Menu.Icon}"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Width="10"
Height="6"
Stretch="Uniform"
IsHitTestVisible="False" />
</Grid>
</Border>
<Popup x:Name="DropDownPopup"
PlacementTarget="{Binding ElementName=DisplayBorder}"
Placement="Bottom"
StaysOpen="False"
AllowsTransparency="True"
Width="{Binding ElementName=DisplayBorder, Path=ActualWidth}"
Opened="DropDownPopup_Opened"
Closed="DropDownPopup_Closed">
<Border Background="{DynamicResource BackgroundColor.Menu.SubCategory}"
CornerRadius="7.5"
Padding="10">
<Border.Effect>
<DropShadowEffect BlurRadius="10"
Color="{DynamicResource DropShadowColor.Menu}"
Direction="55"
ShadowDepth="10"
Opacity="0.35" />
</Border.Effect>
<StackPanel>
<Grid Margin="0 0 10 0">
<TextBox x:Name="SearchTextBox"
Style="{DynamicResource Customizable.Editable.TextBox.EditOnly}"
Background="{Binding ElementName=HierarchySelector, Path=ComboBoxBackground}"
Padding="27 5 5 5"
VerticalContentAlignment="Center"
TextChanged="SearchTextBox_TextChanged" />
<ico:AdaptableIcon SelectedInternIcon="menuBar_search"
Style="{DynamicResource Menu.MenuBar.PinnedIcon.Base}"
BorderPadding="5 5 5 7"
Margin="0"
IconBackgroundColor="Transparent"
HorizontalAlignment="Left"
VerticalAlignment="Center" />
<TextBlock Margin="30 0 0 0"
Foreground="{DynamicResource FontColor.DetailsPage.DataHistory.Date}"
IsHitTestVisible="False"
VerticalAlignment="Center">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=SearchTextBox}"
Value="">
<Setter Property="Text"
Value="{Binding ElementName=HierarchySelector, Path=SearchPlaceholderText}" />
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
<Border Margin="0 10 0 0"
Background="{DynamicResource BackgroundColor.Menu.SubCategory}"
BorderThickness="0"
CornerRadius="5">
<ScrollViewer MaxHeight="300"
VerticalScrollBarVisibility="Auto">
<TreeView x:Name="TreeViewControl"
Background="{Binding ElementName=HierarchySelector, Path=ComboBoxBackground}"
ItemsSource="{Binding VisibleItems, ElementName=HierarchySelector}"
SelectedItemChanged="TreeViewControl_SelectedItemChanged">
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded"
Value="{Binding IsExpanded, Mode=TwoWay}" />
</Style>
<HierarchicalDataTemplate DataType="{x:Type models:HierarchicalSelectionItem}"
ItemsSource="{Binding Children}">
<TextBlock Text="{Binding DisplayName}"
Foreground="{DynamicResource FontColor.Menu.Categories}"
Margin="0 2 0 2" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</ScrollViewer>
</Border>
</StackPanel>
</Border>
</Popup>
<ComboBox x:Name="ComboBoxControl"
Background="{Binding ElementName=HierarchySelector, Path=ComboBoxBackground}"
BorderBrush="{Binding ElementName=HierarchySelector, Path=BorderBrush}"
Foreground="{DynamicResource FontColor.Menu.Categories}"
ItemsSource="{Binding VisibleItems, ElementName=HierarchySelector}"
SelectedItem="{Binding SelectedItem, ElementName=HierarchySelector, Mode=TwoWay}"
DisplayMemberPath="FullPath"
Template="{StaticResource HierarchicalComboBoxTemplate}"
DropDownOpened="ComboBoxControl_DropDownOpened"
DropDownClosed="ComboBoxControl_DropDownClosed"
IsEditable="False"
Padding="0" />
</Grid>
</UserControl>

View File

@@ -8,6 +8,7 @@ using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using static C4IT.Logging.cLogManager;
namespace FasdDesktopUi.Basics.UserControls
{
@@ -17,6 +18,10 @@ namespace FasdDesktopUi.Basics.UserControls
private readonly Dictionary<string, HierarchicalSelectionItem> itemLookup = new Dictionary<string, HierarchicalSelectionItem>();
private readonly DispatcherTimer searchDelayTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(250) };
private string lastSearchText = string.Empty;
private TextBox searchTextBox;
private TreeView treeViewControl;
public ObservableCollection<HierarchicalSelectionItem> VisibleItems => visibleItems;
public event EventHandler DropDownOpened;
@@ -25,10 +30,18 @@ namespace FasdDesktopUi.Basics.UserControls
public HierarchicalSelectionControl()
{
InitializeComponent();
TreeViewControl.ItemsSource = visibleItems;
searchDelayTimer.Tick += SearchDelayTimer_Tick;
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
EnsureTemplateParts();
if (treeViewControl != null)
treeViewControl.ItemsSource = VisibleItems;
UpdateDisplaySelection();
}
#region DependencyProperties
public ObservableCollection<HierarchicalSelectionItem> ItemsSource
@@ -93,16 +106,19 @@ namespace FasdDesktopUi.Basics.UserControls
#endregion
private void DropDownPopup_Opened(object sender, EventArgs e)
private void ComboBoxControl_DropDownOpened(object sender, EventArgs e)
{
SearchTextBox.Focus();
SearchTextBox.SelectAll();
EnsureTemplateParts();
searchTextBox?.Focus();
searchTextBox?.SelectAll();
LogEntry($"[CategoryPicker] DropDownOpened. Selected={SelectedItem?.FullPath ?? "<null>"}");
DropDownOpened?.Invoke(this, e);
}
private void DropDownPopup_Closed(object sender, EventArgs e)
private void ComboBoxControl_DropDownClosed(object sender, EventArgs e)
{
searchDelayTimer.Stop();
LogEntry("[CategoryPicker] DropDownClosed");
DropDownClosed?.Invoke(this, e);
}
@@ -115,7 +131,8 @@ namespace FasdDesktopUi.Basics.UserControls
private void SearchDelayTimer_Tick(object sender, EventArgs e)
{
searchDelayTimer.Stop();
lastSearchText = SearchTextBox.Text ?? string.Empty;
lastSearchText = searchTextBox?.Text ?? string.Empty;
LogEntry($"[CategoryPicker] Search text changed: '{lastSearchText}'");
ApplyFilter(lastSearchText);
}
@@ -124,10 +141,13 @@ namespace FasdDesktopUi.Basics.UserControls
if (e.NewValue is HierarchicalSelectionItem selected)
{
var original = ResolveOriginalItem(selected);
if (original != null)
if (original != null && !Equals(SelectedItem, original))
{
SelectedItem = original;
LogEntry($"[CategoryPicker] Tree selection changed: {original.FullPath}");
}
DropDownPopup.IsOpen = false;
ComboBoxControl.IsDropDownOpen = false;
}
}
@@ -139,7 +159,7 @@ namespace FasdDesktopUi.Basics.UserControls
if (!string.IsNullOrWhiteSpace(item.Id) && itemLookup.TryGetValue(item.Id, out var original))
return original;
return item;
return null;
}
private void RebuildLookup()
@@ -209,13 +229,29 @@ namespace FasdDesktopUi.Basics.UserControls
}
}
private void DisplayBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
private void EnsureTemplateParts()
{
if (!IsEnabled)
return;
if (treeViewControl == null)
{
treeViewControl = ComboBoxControl.Template.FindName("PART_TreeView", ComboBoxControl) as TreeView;
if (treeViewControl != null)
{
treeViewControl.SelectedItemChanged += TreeViewControl_SelectedItemChanged;
treeViewControl.ItemsSource = VisibleItems;
}
}
e.Handled = true;
DropDownPopup.IsOpen = !DropDownPopup.IsOpen;
if (searchTextBox == null)
{
searchTextBox = ComboBoxControl.Template.FindName("PART_SearchTextBox", ComboBoxControl) as TextBox;
if (searchTextBox != null)
searchTextBox.TextChanged += SearchTextBox_TextChanged;
}
}
private void UpdateDisplaySelection()
{
// Display handled by ComboBox DisplayMemberPath (FullPath).
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
@@ -225,16 +261,16 @@ namespace FasdDesktopUi.Basics.UserControls
if (!IsEnabled)
return;
if (!DropDownPopup.IsOpen && (e.Key == Key.Enter || e.Key == Key.Down || e.Key == Key.Space))
if (!ComboBoxControl.IsDropDownOpen && (e.Key == Key.Enter || e.Key == Key.Down || e.Key == Key.Space))
{
DropDownPopup.IsOpen = true;
ComboBoxControl.IsDropDownOpen = true;
e.Handled = true;
return;
}
if (DropDownPopup.IsOpen && e.Key == Key.Escape)
if (ComboBoxControl.IsDropDownOpen && e.Key == Key.Escape)
{
DropDownPopup.IsOpen = false;
ComboBoxControl.IsDropDownOpen = false;
e.Handled = true;
}
}

View File

@@ -166,6 +166,8 @@
ComboBoxBackground="{DynamicResource BackgroundColor.DetailsPage.DataHistory.ValueColumn}"
BorderBrush="{DynamicResource BackgroundColor.Menu.SubCategory.Hover}"
SearchPlaceholderText="{Binding Converter={StaticResource LanguageConverter}, ConverterParameter=Searchbar.Placeholder}"
DropDownOpened="DropDownOpened"
DropDownClosed="DropDownClosed"
PreviewKeyDown="Combobox_PreviewKeyDown" />
<StackPanel>
<TextBlock Text="{Binding Converter={StaticResource LanguageConverter}, ConverterParameter=Dialog.CloseCase.Template}"

View File

@@ -2393,12 +2393,11 @@ namespace FasdDesktopUi.Basics.UserControls
#region DropDown
private static bool IsInsidePageable(FrameworkElement fe) => cUiElementHelper.GetFirstParentOfType<ComboBoxPageable>(fe) != null;
private static bool IsHierarchicalControl(FrameworkElement fe) => fe is HierarchicalSelectionControl;
private void DropDownOpened(object sender, EventArgs e)
{
if (!(sender is FrameworkElement fe)) return;
if (IsInsidePageable(fe) || IsHierarchicalControl(fe)) return; // Spezialfälle handeln Fokus selbst
if (IsInsidePageable(fe)) return; // ComboBoxPageable fired itself
var parentBorder = cUiElementHelper.GetFirstParentOfType<Border>(fe);
if (parentBorder != null)
@@ -2408,7 +2407,7 @@ namespace FasdDesktopUi.Basics.UserControls
private void DropDownClosed(object sender, EventArgs e)
{
if (!(sender is FrameworkElement fe)) return;
if (IsInsidePageable(fe) || IsHierarchicalControl(fe)) return;
if (IsInsidePageable(fe)) return;
var parentBorder = cUiElementHelper.GetFirstParentOfType<Border>(fe);
if (parentBorder != null)