diff --git a/HandyControl/Controls/Input/SearchBar.cs b/HandyControl/Controls/Input/SearchBar.cs index d34a3557..3b13e84e 100644 --- a/HandyControl/Controls/Input/SearchBar.cs +++ b/HandyControl/Controls/Input/SearchBar.cs @@ -7,7 +7,7 @@ using HandyControl.Interactivity; namespace HandyControl.Controls { - public class SearchBar : TextBox + public class SearchBar : TextBox, ICommandSource { public SearchBar() { @@ -51,6 +51,16 @@ namespace HandyControl.Controls { Info = Text }); + + if (Command == null) return; + if (Command is RoutedCommand command) + { + command.Execute(CommandParameter, CommandTarget); + } + else + { + Command.Execute(CommandParameter); + } } /// @@ -67,5 +77,54 @@ namespace HandyControl.Controls get => (bool)GetValue(IsRealTimeProperty); set => SetValue(IsRealTimeProperty, value); } + + public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( + "Command", typeof(ICommand), typeof(SearchBar), new PropertyMetadata(default(ICommand), OnCommandChanged)); + + private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var ctl = (SearchBar)d; + if (e.OldValue is ICommand oldCommand) + { + oldCommand.CanExecuteChanged -= ctl.CanExecuteChanged; + } + if (e.NewValue is ICommand newCommand) + { + newCommand.CanExecuteChanged += ctl.CanExecuteChanged; + } + } + + public ICommand Command + { + get => (ICommand) GetValue(CommandProperty); + set => SetValue(CommandProperty, value); + } + + public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register( + "CommandParameter", typeof(object), typeof(SearchBar), new PropertyMetadata(default(object))); + + public object CommandParameter + { + get => GetValue(CommandParameterProperty); + set => SetValue(CommandParameterProperty, value); + } + + public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register( + "CommandTarget", typeof(IInputElement), typeof(SearchBar), new PropertyMetadata(default(IInputElement))); + + public IInputElement CommandTarget + { + get => (IInputElement) GetValue(CommandTargetProperty); + set => SetValue(CommandTargetProperty, value); + } + + private void CanExecuteChanged(object sender, EventArgs e) + { + if (Command == null) return; + + IsEnabled = Command is RoutedCommand command + ? command.CanExecute(CommandParameter, CommandTarget) + : Command.CanExecute(CommandParameter); + } } } diff --git a/HandyControlDemo/HandyControlDemo.csproj b/HandyControlDemo/HandyControlDemo.csproj index 3c727753..5a62c661 100644 --- a/HandyControlDemo/HandyControlDemo.csproj +++ b/HandyControlDemo/HandyControlDemo.csproj @@ -359,6 +359,7 @@ + diff --git a/HandyControlDemo/UserControl/Controls/SearchBarDemoCtl.xaml b/HandyControlDemo/UserControl/Controls/SearchBarDemoCtl.xaml index 3159c30f..cc484896 100644 --- a/HandyControlDemo/UserControl/Controls/SearchBarDemoCtl.xaml +++ b/HandyControlDemo/UserControl/Controls/SearchBarDemoCtl.xaml @@ -3,11 +3,12 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:HandyControl.Controls;assembly=HandyControl" xmlns:langs="clr-namespace:HandyControlDemo.Properties.Langs" + DataContext="{Binding SearchBarDemo,Source={StaticResource Locator}}" Background="{DynamicResource RegionBrush}"> - + diff --git a/HandyControlDemo/ViewModel/Controls/SearchBarDemoViewModel.cs b/HandyControlDemo/ViewModel/Controls/SearchBarDemoViewModel.cs new file mode 100644 index 00000000..75dccd5f --- /dev/null +++ b/HandyControlDemo/ViewModel/Controls/SearchBarDemoViewModel.cs @@ -0,0 +1,18 @@ +using System; +using GalaSoft.MvvmLight; +using GalaSoft.MvvmLight.Command; +using HandyControl.Controls; + +namespace HandyControlDemo.ViewModel +{ + public class SearchBarDemoViewModel : ViewModelBase + { + public RelayCommand SearchCmd => new Lazy>(() => + new RelayCommand(Search)).Value; + + private void Search(string key) + { + Growl.Info(key); + } + } +} \ No newline at end of file diff --git a/HandyControlDemo/ViewModel/ViewModelLocator.cs b/HandyControlDemo/ViewModel/ViewModelLocator.cs index 960bdef1..800ce4fe 100644 --- a/HandyControlDemo/ViewModel/ViewModelLocator.cs +++ b/HandyControlDemo/ViewModel/ViewModelLocator.cs @@ -25,6 +25,7 @@ namespace HandyControlDemo.ViewModel SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); + SimpleIoc.Default.Register(); } public static ViewModelLocator Instance => new Lazy(() => @@ -54,6 +55,8 @@ namespace HandyControlDemo.ViewModel public DialogDemoViewModel DialogDemo => ServiceLocator.Current.GetInstance(); + public SearchBarDemoViewModel SearchBarDemo => ServiceLocator.Current.GetInstance(); + #endregion } } \ No newline at end of file