Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Avalonia.Controls;

namespace BehaviorsTestApplication.Controls;

public class SingleSelectionTabControl : TabControl
{
protected override Type StyleKeyOverride => typeof(TabControl);

static SingleSelectionTabControl()
{
SelectionModeProperty.OverrideDefaultValue<SingleSelectionTabControl>(SelectionMode.Single);
}
}
15 changes: 9 additions & 6 deletions samples/BehaviorsTestApplication/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
</Style>
</UserControl.Styles>
<DockPanel>
<TextBox x:Name="SearchBox"
Watermark="Search pages..."
Margin="0,0,0,8"
TextChanged="OnSearchTextChanged"
<TextBox x:Name="SearchBox"
Watermark="Search pages..."
Margin="0,0,0,8"
DockPanel.Dock="Top" />
<TextBlock x:Name="NoMatchesText"
Text="No pages match the search."
Margin="0,0,0,8"
IsVisible="False"
DockPanel.Dock="Top" />
<TabControl x:Name="PagesTabControl" Classes="sidebar">
<SingleSelectionTabControl x:Name="PagesTabControl" SelectedIndex="0" Classes="sidebar">
<Interaction.Behaviors>
<SelectingItemsControlSearchBehavior SearchBox="SearchBox"
NoMatchesControl="NoMatchesText" />
</Interaction.Behaviors>
<TabItem Header="CallMethodAction">
<pages:CallMethodActionView />
</TabItem>
Expand Down Expand Up @@ -150,6 +153,6 @@
<TabItem Header="Reactive Navigation">
<pages:ReactiveNavigationView />
</TabItem>
</TabControl>
</SingleSelectionTabControl>
</DockPanel>
</UserControl>
27 changes: 1 addition & 26 deletions samples/BehaviorsTestApplication/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Avalonia.Controls;
using System.Linq;
using Avalonia.Controls;

namespace BehaviorsTestApplication.Views;

Expand All @@ -9,28 +8,4 @@ public MainView()
{
InitializeComponent();
}

private void OnSearchTextChanged(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var query = SearchBox.Text?.ToLowerInvariant() ?? string.Empty;
var visibleCount = 0;

var tabItems = PagesTabControl.Items.OfType<TabItem>().ToList();

foreach (var item in tabItems)
{
var header = item.Header?.ToString()?.ToLowerInvariant() ?? string.Empty;
var visible = header.Contains(query);
item.IsVisible = visible;

if (visible)
{
visibleCount++;
}
}

PagesTabControl.SelectedItem = tabItems.FirstOrDefault(x => x.IsVisible);

NoMatchesText.IsVisible = visibleCount == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Linq;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
/// Filters <see cref="SelectingItemsControl"/> items based on the text of a search box.
/// </summary>
public sealed class SelectingItemsControlSearchBehavior : StyledElementBehavior<SelectingItemsControl>
{
/// <summary>
/// Identifies the <seealso cref="SearchBox"/> avalonia property.
/// </summary>
public static readonly StyledProperty<TextBox?> SearchBoxProperty =
AvaloniaProperty.Register<SelectingItemsControlSearchBehavior, TextBox?>(nameof(SearchBox));

/// <summary>
/// Identifies the <seealso cref="NoMatchesControl"/> avalonia property.
/// </summary>
public static readonly StyledProperty<TextBlock?> NoMatchesControlProperty =
AvaloniaProperty.Register<SelectingItemsControlSearchBehavior, TextBlock?>(nameof(NoMatchesControl));

/// <summary>
/// Gets or sets the search box control.
/// </summary>
[ResolveByName]
public TextBox? SearchBox
{
get => GetValue(SearchBoxProperty);
set => SetValue(SearchBoxProperty, value);
}

/// <summary>
/// Gets or sets the control displayed when no matches are found.
/// </summary>
[ResolveByName]
public Control? NoMatchesControl
{
get => GetValue(NoMatchesControlProperty);
set => SetValue(NoMatchesControlProperty, value);
}

/// <inheritdoc />
protected override void OnAttachedToVisualTree()
{
if (SearchBox is not null)
{
SearchBox.AddHandler(InputElement.TextInputEvent, SearchBox_TextChanged, RoutingStrategies.Bubble);
SearchBox.AddHandler(TextBox.TextChangedEvent, SearchBox_TextChanged, RoutingStrategies.Bubble);
}
}

/// <inheritdoc />
protected override void OnDetachedFromVisualTree()
{
if (SearchBox is not null)
{
SearchBox.RemoveHandler(InputElement.TextInputEvent, SearchBox_TextChanged);
SearchBox.RemoveHandler(TextBox.TextChangedEvent, SearchBox_TextChanged);
}
}

private void SearchBox_TextChanged(object? sender, RoutedEventArgs e)
{
if (AssociatedObject is null)
{
return;
}

var query = SearchBox?.Text?.ToLowerInvariant() ?? string.Empty;
var visibleCount = 0;
var tabItems = AssociatedObject.Items.OfType<TabItem>().ToList();

foreach (var item in tabItems)
{
var header = item.Header?.ToString()?.ToLowerInvariant() ?? string.Empty;
var visible = header.Contains(query);
item.IsVisible = visible;
if (visible)
{
visibleCount++;
}
}

AssociatedObject.SelectedItem = tabItems.FirstOrDefault(x => x.IsVisible);

if (NoMatchesControl is not null)
{
NoMatchesControl.IsVisible = visibleCount == 0;
}
}
}
Loading