c#


Button doesn't execute command MVVM WPF


I have problem with binded command to the button. I cannot execute this command on click, but this code worked fine in few other places.
I'm using MVVM Light Toolkit. Here is DataContext binding and button code.
DataContext="{Binding Source={StaticResource Locator}, Path=UserTodoViewModel}"
<Button HorizontalAlignment="Center"
Margin="5"
Background="Gray" Foreground="White" Command="{Binding SaveCommand}" />
ViewModel:
public ICommand SaveCommand { get; }
SaveCommand =
new RelayCommand(
() => {
// Some logic
MessageBox.Show("Changes saved!", "Project Manager", MessageBoxButton.OK,
MessageBoxImage.Information);
});
Thanks for any help.
Your XAML Code is fine.
But in the Viewmodel you need to edit your command handler. Try this:
Create a class for command handler:
public class CommandHandler : ICommand
{
private Action _action;
private bool _canExecute;
public CommandHandler(Action action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
}
In the Viewmodel use this command property:
public ICommand SaveCommand { get { return new CommandHandler(() => Save(), true); } }
In this way you can reuse the command handler class for another command, also you only one row of code to define the command property
When I've used MVVM Light in the past, here's how I've done button commands
View Model:
public RelayCommand SaveEmployeeCommand
{
get;
private set;
}
// call this method from ctor
private void InitializeCommands()
{
SaveEmployeeCommand = new RelayCommand(
Save,
() => true);
}
private void Save()
{
}
Then in XAML:
<Button Command="{Binding Path=SaveEmployeeCommand}">

Related Links

Exception related to relative paths
array parameter when overriden in method doesn't change outside of it
AES: Use nonce to create new key for HMAC
Xamarin Android aplication doesn't work without debugger
Load data from database to TreeView when parent and child are parts of the same field in C#
How to skip the creating the root node on a treeview?
Add or remove relations one-to-many entity doesn't work
ListBoxItems selected color differs when programatically selecting the item
Reusing the connection and prepare statement to mysql database
Play audio from a stream in UWP
EWS MVC where save message
lambda expression and the model keyword cannot be used in strongly typed views inside a class library [duplicate]
Return JSON data from webservice [duplicate]
.net standard doesn't resolve child assemblies
How do I constraint a generic type parameter to only accept nullable value types in C#?
Object reference not set to an instance of an object for my Database object. C# [duplicate]

Categories

HOME
reactjs
ssms
url
google-cloud-pubsub
cors
wifi
glsl
yuv
autocad
clip-path
angularjs-ng-repeat
fetch
derby
hql
facebook-marketing-api
openam
airbnb
xna
datazen-server
javaagents
struts
poisson
deployment-descriptor
rancher
utc
stack-trace
proof-general
bytebuffer
paper-trail-gem
visual-prolog
luhn
eclipse-jdt
n1ql
campaign-monitor
gstreamer-1.0
framebuffer
nsdateformatter
nscollectionview
mediastream
file-conversion
definition
servicebus
ocpjp
weld
ack
anonymous-function
infovis
django-tables2
lucee
e4
webmatrix
sharing
settext
hoare-logic
hotkeys
phpdesigner
custom-build-step
ideavim
instabug
mojolicious
javapackager
jeet-grid
httpmodule
chrome-mobile
fiware-health
alpha-beta-pruning
dojox.grid
drools-guvnor
jsonschema2pojo
winrun4j
maven-javadoc-plugin
grunt-wiredep
fade
ironworker
ia-32
xrandr
mandelbrot
mmo
vline
screensharing
diazo
jmesa
rjs
jquery-click-event
jqtransform
http-response-codes
browser-state
dynamic-scope
cstring
unauthorized
memory-consumption

Resources

Encrypt Message