Custom Control CommandParameter

BigH61 581 Reputation points
2023-03-29T13:18:09.57+00:00

I am working on a Custom Control that Consists of a ComboBox and a Button to the right as the image below.

Control

My code for allowing Commands to be Bound to the Button is:


private Button _SearchButton;

        // events exposed to container
        public static readonly RoutedEvent SearchClickEvent =
            EventManager.RegisterRoutedEvent("SearchClick", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(SearchComboBox));

        // get the button objects as the templete is applied and add click event handlers
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _SearchButton = GetTemplateChild("SearchButton") as Button;
            if (_SearchButton != null) _SearchButton.Click += SearchButtonClicked;

            this.Loaded += SearchComboBox_Loaded;
        }

        // expose and raise 'SearchClick' event
        public event RoutedEventHandler SearchClick
        {
            add { AddHandler(SearchClickEvent, value); }
            remove { RemoveHandler(SearchClickEvent, value); }
        }

        private void SearchButtonClicked(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(SearchClickEvent));
        }

        private void SearchComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            if (MaxDropDownWidth == 0)
            {
                MaxDropDownWidth = this.ActualWidth;
            }
        }

        #endregion Routed Event

        #region Search Button Command

        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command",
        typeof(ICommand), typeof(SearchComboBox), new PropertyMetadata(null, OnCommandPropertyChanged));

        private static void OnCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SearchComboBox? control = d as SearchComboBox;
            if (control == null) return;

            control.SearchClick -= SearchButtonClick;
            control.SearchClick += SearchButtonClick;
        }

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        private static void SearchButtonClick(object sender, RoutedEventArgs e)
        {
            SearchComboBox? control = sender as SearchComboBox;
            if (control == null || control.Command == null) return;

            ICommand command = control.Command;

            if (command.CanExecute(null))
                command.Execute(null);
        }

        #endregion Search Button Command

I Would also like to have the option to pass a CommandParameter but I am struggling with this. Any assistance would be much appreciated.

Developer technologies Windows Presentation Foundation
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. BigH61 581 Reputation points
    2023-03-29T19:34:45.6433333+00:00

    This is working for me

        public object CommandParameter
    
        {
    
            get { return (object)GetValue(CommandParameterProperty); }
    
            set { SetValue(CommandParameterProperty, value); }
    
        }
    
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(nameof(CommandParameter), 
    
            typeof(object), typeof(SearchComboBox), new PropertyMetadata(string.Empty));
    
        #endregion Search Button CommandParameter
    
    
    private static void SearchButtonClick(object sender, RoutedEventArgs e)
            {
                SearchComboBox? control = sender as SearchComboBox;
                if (control == null || control.Command == null) return;
    
                ICommand command = control.Command;
    
                if (command.CanExecute(control.CommandParameter))
                    command.Execute(control.CommandParameter);
            }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.