Note that there are some explanatory texts on larger screens.

plurals
  1. POICommand Parameters always null
    text
    copied!<p>I'm trying to fetch some of my UI content (text from textboxes) as parameters into my ICommand method, located in my ViewModel. </p> <p>First of all, I got this RelayCommand implementation:</p> <pre><code>/// &lt;summary&gt; /// A command whose sole purpose is to relay its functionality to other /// objects by invoking delegates. The default return value for the /// CanExecute method is 'true'. /// &lt;/summary&gt; public class RelayCommand : ICommand { #region Fields readonly Action&lt;object&gt; _execute; readonly Predicate&lt;object&gt; _canExecute; #endregion // Fields #region Constructors /// &lt;summary&gt; /// Creates a new command that can always execute. /// &lt;/summary&gt; /// &lt;param name="execute"&gt;The execution logic.&lt;/param&gt; public RelayCommand(Action&lt;object&gt; execute) : this(execute, null) { } /// &lt;summary&gt; /// Creates a new command. /// &lt;/summary&gt; /// &lt;param name="execute"&gt;The execution logic.&lt;/param&gt; /// &lt;param name="canExecute"&gt;The execution status logic.&lt;/param&gt; public RelayCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute) { if (execute == null) throw new ArgumentNullException("execute"); _execute = execute; _canExecute = canExecute; } #endregion // Constructors #region ICommand Members [DebuggerStepThrough] public bool CanExecute(object parameters) { return _canExecute == null ? true : _canExecute(parameters); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameters) { _execute(parameters); } #endregion // ICommand Members } </code></pre> <p>I declared a command in my ViewModel like this:</p> <pre><code>public ICommand AddEntityCommand { get { if(_addEntity == null) { _addEntity = new RelayCommand(AddEntityToDb); } return _addEntity; } } </code></pre> <p>That's my xaml definition:</p> <pre><code>&lt;Label Content="Entity Name:" Name="label1"/&gt; &lt;TextBox Name="textBox_EntityName" /&gt; &lt;Label Content="Entity Type:" Name="label2" /&gt; &lt;TextBox Name="textBox_EntityType" /&gt; &lt;Button Content="Add" Name="btnAdd" Command="{Binding Path=AddEntityCommand}"&gt; &lt;Button.CommandParameter&gt; &lt;MultiBinding Converter="{StaticResource MultiParamConverter}"&gt; &lt;Binding Path="Text" ElementName="textBox_EntityName" /&gt; &lt;Binding Path="Text" ElementName="textBox_EntityType" /&gt; &lt;/MultiBinding&gt; &lt;/Button.CommandParameter&gt; &lt;/Button&gt; </code></pre> <p>and finally that's my Converter:</p> <pre><code>public class MultiParamConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return (object)values; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p>I debugged the tool several times, and the debugger stops within the converter as soon as I changed a value in a text box. In this case, the values from the UI are shown within the </p> <pre><code>object[] values </code></pre> <p>parameter. Clicking the Button, wouldn't let me stop within the convert but it calls the method</p> <pre><code>AddEntityToDb </code></pre> <p>correctly, but the parameter, which has the type object[], contains always two elements which are both null.</p> <p>I think I did something terribly wrong creating the AddEntityCommand, but I can't figure it out on my own. But what is the reason that the parameter of AddEntityToDb contains always two null elements? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload