Skip to content

Commit

Permalink
Add StructsTreeView_SelectedItemChanged to call item.BringIntoView.
Browse files Browse the repository at this point in the history
This scrolls to the item selected when found by a search, but it only works if the item's ancestors are expanded.
  • Loading branch information
ScottHutchinson committed Sep 27, 2023
1 parent b5679f5 commit eb1b356
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 3 additions & 1 deletion NG-DART.StructFilters.Views/StructFiltersWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@
x:Name="StructsTreeView"
Grid.Row="1"
Margin="10,10,10,10"
ItemsSource="{Binding Fields}">
ItemsSource="{Binding Fields}"
>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Focusable" Value="False" />
<Setter Property="IsEnabled" Value="{Binding IsEnabled}" />
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
<EventSetter Event="Selected" Handler="StructsTreeView_SelectedItemChanged" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
Expand Down
15 changes: 13 additions & 2 deletions NG-DART.StructFilters.Views/StructFiltersWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System.Windows.Controls;

namespace NGDartStructFilters.Views {
/// <summary>
Expand All @@ -8,5 +9,15 @@ public partial class StructFiltersWindow : Window {
public StructFiltersWindow() {
InitializeComponent();
}
}
}

// Based on https://stackoverflow.com/a/9494484/5652483
private void StructsTreeView_SelectedItemChanged(object sender, RoutedEventArgs e) {
if (sender is TreeViewItem item) {
item.BringIntoView();
e.Handled = true;
}
}

} // class StructFiltersWindow

} // namespace NGDartStructFilters.Views
6 changes: 3 additions & 3 deletions NG-DART.StructFilters/StructFilters.fs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ module App =
let caseInsensitiveContains (haystack: string) (needle: string) =
haystack.IndexOf(needle, System.StringComparison.OrdinalIgnoreCase) >= 0

let isItemSelected (searchText: string) (fd: FieldData) =
let isFoundInSearch (searchText: string) (fd: FieldData) =
if System.String.IsNullOrWhiteSpace searchText then
false
else
Expand Down Expand Up @@ -377,10 +377,10 @@ module App =
"ContextCancel" |> Binding.cmd (LeafMsg ContextCancel)
"IsEnabled" |> Binding.oneWay(fun ((m: Model), _) -> not m.IsEmpty)
"IsExpanded" |> Binding.oneWay(fun ((m: Model), _) ->
level < m.ExpandLevel
level < m.ExpandLevel // TODO: || is an ancestor of the selected item found in a search
)
"IsSelected" |> Binding.oneWay(fun ((m: Model), { Self = (s: RoseTree<FieldData>) }) ->
let isSelected = isItemSelected m.SearchEnterText s.Data
let isSelected = isFoundInSearch m.SearchEnterText s.Data
if isSelected then
Debug.WriteLine($"****** Name: {s.Data.Name}, Type: {s.Data.Type} selected")
isSelected
Expand Down

0 comments on commit eb1b356

Please sign in to comment.