63 lines
2.5 KiB
C#
63 lines
2.5 KiB
C#
using System.Windows;
|
|
using System.Windows.Input;
|
|
using ActiproSoftware.Windows.Controls.Editors;
|
|
|
|
namespace JD.Guinness.WPF.Module.Behavior
|
|
{
|
|
public static class AutoCompleteBoxBehavior
|
|
{
|
|
public static readonly DependencyProperty ShowAllOnOpenProperty;
|
|
|
|
static AutoCompleteBoxBehavior()
|
|
{
|
|
ShowAllOnOpenProperty = DependencyProperty.RegisterAttached("ShowAllOnOpen", typeof(bool), typeof(AutoCompleteBoxBehavior), new PropertyMetadata(OnShowAllOnOpenChanged));
|
|
}
|
|
|
|
private static void OnShowAllOnOpenChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
|
|
{
|
|
if (target is AutoCompleteBox autoCompleteBox)
|
|
{
|
|
if (autoCompleteBox.GetShowAllOnOpen())
|
|
{
|
|
autoCompleteBox.PreviewKeyDown += OnAutoCompleteBoxPreviewKeyDown;
|
|
autoCompleteBox.PreviewMouseLeftButtonDown += OnAutoCompleteBoxMouseLeftButtonDown;
|
|
autoCompleteBox.TextChanged += OnAutoCompleteBoxTextChanged;
|
|
}
|
|
else
|
|
{
|
|
autoCompleteBox.PreviewKeyDown -= OnAutoCompleteBoxPreviewKeyDown;
|
|
autoCompleteBox.PreviewMouseLeftButtonDown -= OnAutoCompleteBoxMouseLeftButtonDown;
|
|
autoCompleteBox.TextChanged -= OnAutoCompleteBoxTextChanged;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void OnAutoCompleteBoxPreviewKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (sender is AutoCompleteBox autoCompleteBox && !autoCompleteBox.DataFilter.IsEnabled)
|
|
{
|
|
autoCompleteBox.DataFilter.IsEnabled = true;
|
|
}
|
|
}
|
|
|
|
private static void OnAutoCompleteBoxMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
|
{
|
|
if (sender is AutoCompleteBox autoCompleteBox && !autoCompleteBox.IsPopupOpen)
|
|
{
|
|
autoCompleteBox.DataFilter.IsEnabled = false;
|
|
}
|
|
}
|
|
|
|
private static void OnAutoCompleteBoxTextChanged(object sender, AutoCompleteBoxEventArgs e)
|
|
{
|
|
if (sender is AutoCompleteBox autoCompleteBox && string.IsNullOrEmpty(autoCompleteBox.Text))
|
|
{
|
|
autoCompleteBox.SelectedItem = null;
|
|
}
|
|
}
|
|
|
|
public static bool GetShowAllOnOpen(this DependencyObject obj) => (bool)obj.GetValue(ShowAllOnOpenProperty);
|
|
|
|
public static void SetShowAllOnOpen(this DependencyObject obj, bool value) => obj.SetValue(ShowAllOnOpenProperty, value);
|
|
}
|
|
}
|