GHauan
GHauan

Reputation: 189

What parameter does CommandBehaviorBase.ExcuteCommand() take in PRISM 6

I am in the process of upgrading a WPF-application from using PRISM 4 to use PRISM 6.1. This application uses a class that has CommandBehaviorBase as its base-class. It calls a method called ExecuteCommand. In an article discussing upgrading to PRISM 5 (Upgrade from 4.1 to 5), I see that this method now takes a parameter. (The CommandBehaviorBase class was moved to the Prism.Interactivity namespace from the Commands namespace. The ExecuteCommand method now takes an object as a parameter. ). My question is what object I should pass to that method?

The code for the class as it is now:

/// <summary>
/// Defines a behavior that executes a <see cref="ICommand"/> when the Return key is pressed inside a <see cref="TextBox"/>.
/// </summary>
/// <remarks>This behavior also supports setting a basic watermark on the <see cref="TextBox"/>.</remarks>
public class ReturnCommandBehavior : CommandBehaviorBase<TextBox>
{
    /// <summary>
    /// Initializes a new instance of <see cref="ReturnCommandBehavior"/>.
    /// </summary>
    /// <param name="textBox">The <see cref="TextBox"/> over which the <see cref="ICommand"/> will work.</param>
    public ReturnCommandBehavior(TextBox textBox)
        : base(textBox)
    {
        textBox.AcceptsReturn = false;
        textBox.KeyDown += (s, e) => this.KeyPressed(e.Key);
        textBox.GotFocus += (s, e) => this.GotFocus();
        textBox.LostFocus += (s, e) => this.LostFocus();
    }

    /// <summary>
    /// Gets or Sets the text which is set as water mark on the <see cref="TextBox"/>.
    /// </summary>
    public string DefaultTextAfterCommandExecution { get; set; }

    /// <summary>
    /// Executes the <see cref="ICommand"/> when <paramref name="key"/> is <see cref="Key.Enter"/>.
    /// </summary>
    /// <param name="key">The key pressed on the <see cref="TextBox"/>.</param>
    protected void KeyPressed(Key key)
    {
        if (key == Key.Enter && TargetObject != null)
        {
            this.CommandParameter = TargetObject.Text;
            ExecuteCommand();


            this.ResetText();
        }
    }

    private void GotFocus()
    {
        if (TargetObject != null && TargetObject.Text == this.DefaultTextAfterCommandExecution)
        {
            this.ResetText();
        }
    }

    private void ResetText()
    {
        TargetObject.Text = string.Empty;
    }

    private void LostFocus()
    {
        if (TargetObject != null && string.IsNullOrEmpty(TargetObject.Text) && this.DefaultTextAfterCommandExecution != null)
        {
            TargetObject.Text = this.DefaultTextAfterCommandExecution;
        }
    }
}

Grateful for any help on this.

regards Gert

Upvotes: 0

Views: 219

Answers (1)

Haukinger
Haukinger

Reputation: 10863

Intuitively, I'd say, "the parameter of the command to execute".

Looking at the code, it seems to be like that (line 121):

https://github.com/PrismLibrary/Prism/blob/75206c591ce547d7e78787f1ba52cb97257052a5/Source/Wpf/Prism.Wpf/Interactivity/CommandBehaviorBase.cs

If you use CommandParameter, just pass null, as it will be ignored anyway.

Upvotes: 1

Related Questions