Maybe this is a dumb question, but I can't find the answer: in the following xaml what does CommandParameter
binding to? Or in general, what does "{Binding}"
mean?
<Button Command="{Binding DataContext.DeleteCommand, ElementName=List}"
CommandParameter="{Binding}"/>
CommandParameter - represents a user-defined data value that can be passed to the command when it is executed. CommandTarget - the object on which the command is being executed.
Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.
WPF binding offers four types of Binding. Remember, Binding runs on UI thread unless otherwise you specify it to run otherwise. OneWay: The target property will listen to the source property being changed and will update itself.
Passing a parameter to the CanExecute and Execute methods A parameter can be passed through the "CommandParameter" property. Once the button is clicked the selected address value is passed to the ICommand. Execute method. The CommandParameter is sent to both CanExecute and Execute events.
{Binding ...}
is a MarkupExtension.
In its usual form it takes a Path like {Binding Path=someProperty, ...}
(or its short form {Binding someProperty, ...}
).
So the path in {Binding}
is empty which means the Binding is bound to whatever Source there is for the Binding. This might be a little easier to understand if you know, that {Binding}
is actually the same as {Binding DataContext,RelativeSource={RelativeSource Self}}
.
So in your case CommandParameter gets the value of the current DataContext of the Button.
An Empty {Binding}
will pass the current DataContext of the control to the Executed and CanExecute methods respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With