mirror of
https://github.com/VitalickS/BrightSharp.Toolkit.git
synced 2026-03-21 02:21:15 +00:00
.net copy
This commit is contained in:
117
BrightSharp.NET/Behaviors/FilterDefaultViewTextBoxBehavior.cs
Normal file
117
BrightSharp.NET/Behaviors/FilterDefaultViewTextBoxBehavior.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace BrightSharp.Behaviors
|
||||
{
|
||||
public class FilterDefaultViewTextBoxBehavior : Behavior<TextBox>
|
||||
{
|
||||
readonly DispatcherTimer _timer = new DispatcherTimer();
|
||||
|
||||
public FilterDefaultViewTextBoxBehavior()
|
||||
{
|
||||
_timer.Tick += Timer_Tick;
|
||||
FilterDelay = TimeSpan.FromSeconds(1);
|
||||
IgnoreCase = null; //Case sensitive if any char upper case
|
||||
FilterStringPropertyName = "FilterString";
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void Timer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
_timer.Stop();
|
||||
if (ItemsSource != null)
|
||||
{
|
||||
var view = CollectionViewSource.GetDefaultView(ItemsSource);
|
||||
if (view is ListCollectionView)
|
||||
{
|
||||
var listCollectionView = (ListCollectionView)view;
|
||||
if (listCollectionView.IsAddingNew || listCollectionView.IsEditingItem) return;
|
||||
}
|
||||
view.Filter = CustomFilter ?? GetDefaultFilter(ItemsSource.GetType().GetGenericArguments()[0]);
|
||||
}
|
||||
}
|
||||
|
||||
public string FilterStringPropertyName { get; set; }
|
||||
|
||||
public bool HasFilterText
|
||||
{
|
||||
get { return (bool)GetValue(HasFilterTextProperty); }
|
||||
set { SetValue(HasFilterTextProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty HasFilterTextProperty =
|
||||
DependencyProperty.Register("HasFilterText", typeof(bool), typeof(FilterDefaultViewTextBoxBehavior), new PropertyMetadata(false));
|
||||
|
||||
|
||||
|
||||
public IEnumerable ItemsSource
|
||||
{
|
||||
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
|
||||
set { SetValue(ItemsSourceProperty, value); }
|
||||
}
|
||||
|
||||
// Using a DependencyProperty as the backing store for ItemsSource. This enables animation, styling, binding, etc...
|
||||
public static readonly DependencyProperty ItemsSourceProperty =
|
||||
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(FilterDefaultViewTextBoxBehavior), new PropertyMetadata(null));
|
||||
|
||||
|
||||
public bool? IgnoreCase { get; set; }
|
||||
private Predicate<object> GetDefaultFilter(Type filterItemType)
|
||||
{
|
||||
Func<object, string> dataFunc;
|
||||
var pInfo = filterItemType.GetProperty(FilterStringPropertyName);
|
||||
if (pInfo == null)
|
||||
{
|
||||
dataFunc = (x) => string.Join(",", filterItemType.GetProperties().Where(p => !p.Name.EndsWith("Id")).Select(p => (p.GetValue(x, null) ?? string.Empty).ToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
dataFunc = (x) => (pInfo.GetValue(x, null) ?? string.Empty).ToString();
|
||||
}
|
||||
var filterText = AssociatedObject.Text;
|
||||
var ic = IgnoreCase ?? !filterText.Any(char.IsUpper);
|
||||
return x =>
|
||||
{
|
||||
if (x == null || string.IsNullOrEmpty(filterText)) return true;
|
||||
var propValStr = dataFunc(x);
|
||||
foreach (var item in filterText.Split(';'))
|
||||
{
|
||||
if (propValStr.IndexOf(item, ic ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == -1)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
public Predicate<object> CustomFilter { get; set; }
|
||||
|
||||
public TimeSpan FilterDelay { get { return _timer.Interval; } set { _timer.Interval = value; } }
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
base.OnAttached();
|
||||
HasFilterText = !string.IsNullOrEmpty(AssociatedObject.Text);
|
||||
AssociatedObject.TextChanged += AssociatedObject_TextChanged;
|
||||
}
|
||||
|
||||
private void AssociatedObject_TextChanged(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
_timer.Stop(); _timer.Start();
|
||||
HasFilterText = !string.IsNullOrEmpty(AssociatedObject.Text);
|
||||
}
|
||||
|
||||
protected override void OnDetaching()
|
||||
{
|
||||
AssociatedObject.TextChanged -= AssociatedObject_TextChanged;
|
||||
_timer.Tick -= Timer_Tick;
|
||||
base.OnDetaching();
|
||||
}
|
||||
}
|
||||
}
|
||||
80
BrightSharp.NET/Behaviors/MinMaxSize_Logic.cs
Normal file
80
BrightSharp.NET/Behaviors/MinMaxSize_Logic.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using BrightSharp.Interop;
|
||||
using BrightSharp.Interop.Constants;
|
||||
using BrightSharp.Interop.Structures;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
|
||||
namespace BrightSharp.Behaviors
|
||||
{
|
||||
internal class MinMaxSize_Logic
|
||||
{
|
||||
private HwndSource hwndSource;
|
||||
private Window associatedObject;
|
||||
public MinMaxSize_Logic(Window associatedObject)
|
||||
{
|
||||
this.associatedObject = associatedObject;
|
||||
}
|
||||
public void OnAttached()
|
||||
{
|
||||
this.associatedObject.SourceInitialized += AssociatedObject_SourceInitialized;
|
||||
}
|
||||
|
||||
private void AssociatedObject_SourceInitialized(object sender, EventArgs e)
|
||||
{
|
||||
IntPtr handle = new WindowInteropHelper(this.associatedObject).Handle;
|
||||
hwndSource = HwndSource.FromHwnd(handle);
|
||||
hwndSource.AddHook(WindowProc);
|
||||
}
|
||||
|
||||
public void OnDetaching()
|
||||
{
|
||||
this.associatedObject.SourceInitialized -= AssociatedObject_SourceInitialized;
|
||||
if (hwndSource != null)
|
||||
{
|
||||
hwndSource.RemoveHook(WindowProc);
|
||||
hwndSource.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
switch (msg)
|
||||
{
|
||||
case 0x0024:
|
||||
WmGetMinMaxInfo(hwnd, lParam);
|
||||
handled = true;
|
||||
break;
|
||||
}
|
||||
return (IntPtr)0;
|
||||
}
|
||||
|
||||
private void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
|
||||
{
|
||||
var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
|
||||
|
||||
// Adjust the maximized size and position to fit the work area of the correct monitor
|
||||
IntPtr monitor = NativeMethods.MonitorFromWindow(hwnd, (int)MonitorFromWindowFlags.MONITOR_DEFAULTTONEAREST);
|
||||
|
||||
if (monitor != IntPtr.Zero)
|
||||
{
|
||||
var monitorInfo = new MONITORINFO();
|
||||
NativeMethods.GetMonitorInfo(monitor, monitorInfo);
|
||||
RECT rcWorkArea = monitorInfo.rcWork;
|
||||
RECT rcMonitorArea = monitorInfo.rcMonitor;
|
||||
mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.Left - rcMonitorArea.Left);
|
||||
mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.Top - rcMonitorArea.Top);
|
||||
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.Right - rcWorkArea.Left);
|
||||
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top);
|
||||
mmi.ptMinTrackSize.x = (int)associatedObject.MinWidth;
|
||||
mmi.ptMinTrackSize.y = (int)associatedObject.MinHeight;
|
||||
}
|
||||
|
||||
Marshal.StructureToPtr(mmi, lParam, true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
33
BrightSharp.NET/Behaviors/SelectAllTextOnFocusBehavior.cs
Normal file
33
BrightSharp.NET/Behaviors/SelectAllTextOnFocusBehavior.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace BrightSharp.Behaviors
|
||||
{
|
||||
public class SelectAllTextOnFocusBehavior : Behavior<TextBoxBase>
|
||||
{
|
||||
protected override void OnAttached() {
|
||||
base.OnAttached();
|
||||
AssociatedObject.GotKeyboardFocus += AssociatedObjectGotKeyboardFocus;
|
||||
AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObjectPreviewMouseLeftButtonDown;
|
||||
}
|
||||
|
||||
protected override void OnDetaching() {
|
||||
base.OnDetaching();
|
||||
AssociatedObject.GotKeyboardFocus -= AssociatedObjectGotKeyboardFocus;
|
||||
AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObjectPreviewMouseLeftButtonDown;
|
||||
}
|
||||
|
||||
private void AssociatedObjectGotKeyboardFocus(object sender,
|
||||
KeyboardFocusChangedEventArgs e) {
|
||||
AssociatedObject.SelectAll();
|
||||
}
|
||||
|
||||
private void AssociatedObjectPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
|
||||
if (!AssociatedObject.IsKeyboardFocusWithin) {
|
||||
AssociatedObject.Focus();
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
23
BrightSharp.NET/Behaviors/WindowMinMaxSizeBehavior.cs
Normal file
23
BrightSharp.NET/Behaviors/WindowMinMaxSizeBehavior.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
using System.Windows;
|
||||
|
||||
namespace BrightSharp.Behaviors
|
||||
{
|
||||
public class WindowMinMaxSizeBehavior : Behavior<Window>
|
||||
{
|
||||
private MinMaxSize_Logic _logic;
|
||||
protected override void OnAttached()
|
||||
{
|
||||
base.OnAttached();
|
||||
_logic = new MinMaxSize_Logic(AssociatedObject);
|
||||
_logic.OnAttached();
|
||||
}
|
||||
|
||||
|
||||
protected override void OnDetaching()
|
||||
{
|
||||
_logic.OnDetaching();
|
||||
base.OnDetaching();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user