Questions tagged [icommand]

Interface for MVVM scenarios in Silverlight and WPF to bind commands from models to controls (Buttons).

icommand
Filter by
Sorted by
Tagged with
73 votes
4 answers
159k views

ICommand MVVM implementation

So in this particular MVVM implementation I'm doing, I need several commands. I really got tired of implementing the ICommand classes one by one, so I came up with a solution, but I don't know how ...
Carlo's user avatar
  • 25.8k
45 votes
5 answers
14k views

Is Josh Smith's implementation of the RelayCommand flawed?

Consider the reference Josh Smith' article WPF Apps With The Model-View-ViewModel Design Pattern, specifically the example implementation of a RelayCommand (In Figure 3). (No need to read through the ...
Greg D's user avatar
  • 43.7k
39 votes
3 answers
15k views

What is the actual task of CanExecuteChanged and CommandManager.RequerySuggested?

I got the following code from Josh Smith's MVVM tutorial. Can anyone provide a quick explanation of what this code actually does? public event EventHandler CanExecuteChanged { add { ...
Aryan SuryaWansi's user avatar
34 votes
7 answers
23k views

CommandManager.InvalidateRequerySuggested() isn't fast enough. What can I do?

Short Version Calls to CommandManager.InvalidateRequerySuggested() take far longer to take effect than I would like (1-2 second delay before UI controls become disabled). Long Version I have a ...
Rob's user avatar
  • 26.2k
27 votes
2 answers
33k views

CanExecuteChanged event of ICommand

Icommand contains two methods and one event. What the two methods do is clear, but I can’t understand what the event does that is provided in ICommand. When is the CanExecuteChanged event raised? ...
Pritesh's user avatar
  • 3,238
13 votes
5 answers
18k views

WPF ViewModel Commands CanExecute issue

I'm having some difficulty with Context Menu commands on my View Model. I'm implementing the ICommand interface for each command within the View Model, then creating a ContextMenu within the ...
LiamV's user avatar
  • 1,136
84 votes
5 answers
88k views

Command for WPF TextBox that fires up when we hit Enter Key

It is very easy to bind Buttons in WPF apps to Commands in a VIEWMODEL class. I'd like to achieve a similar binding for a TextBox. I have a TextBox and I need to bind it to a Command that fires up ...
Aryan SuryaWansi's user avatar
35 votes
4 answers
173k views

Pass command parameter to method in ViewModel in WPF?

I am trying to pass CommandParameter to the method in my ViewModel. How to do this? private void Open(object sender) { if (sender==this.objMainWindow.btnHistory) { objMainWindow....
Mahsa's user avatar
  • 497
17 votes
2 answers
22k views

How can I pass the event argument to a command using triggers?

So I have a simple setup, an autocompletebox with its Populating event that I want to bind to a command. I use clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity (is ...
Trident D'Gao's user avatar
14 votes
6 answers
63k views

How to Bind a Command in WPF

Sometimes we used complex ways so many times, we forgot the simplest ways to do the task. I know how to do command binding, but i always use same approach. Create a class that implements ICommand ...
Manvinder's user avatar
  • 4,537
52 votes
2 answers
43k views

ICommand vs RoutedCommand

Let's have a button Command property bound to a custom command. When should I implement ICommand and when derive from RoutedCommand? I see that RoutedCommand implements ICommand. In which case could ...
theSpyCry's user avatar
  • 12.2k
22 votes
2 answers
44k views

Pass command parameter from the xaml

I try to do something like this: <DataGrid Name="myGrid" ItemSource="{Binding Path=MyCollection}"> <DataGrid.ContextMenu> <ContextMenu> <MenuItem ...
Maya's user avatar
  • 1,009
21 votes
7 answers
31k views

binding a command inside a listbox item to a property on the viewmodel parent

I've been working on this for about an hour and looked at all related SO questions. My problem is very simple: I have HomePageVieModel: HomePageVieModel +IList<NewsItem> AllNewsItems +...
gideon's user avatar
  • 19.4k
12 votes
5 answers
17k views

Relay Command can execute and a Task

i want to start a task when a relay command is called, however i want to disable the button as long as that task is running take this example private ICommand update; public ICommand Update {...
FPGA's user avatar
  • 3,705
10 votes
4 answers
501 views

What is the real advantage of keeping code out of the XAML code behind?

There is a lot of effort in the Silverlight community to keep a XAML's code behind file as free of code as possible. What is the real motivation behind this? For example, what is the advantage of ...
Matt Greer's user avatar
  • 61.4k
8 votes
3 answers
15k views

WPF and MVVM. Binding Events

I'm developing a WPF application with the MVVM pattern, RelayCommand, etc. I read a lot on this question but I am not clear as to: All I want to do is move a shape, like an ellipse, for example, and ...
Dani O.'s user avatar
  • 89
2 votes
4 answers
2k views

WPF: Update button if text in TextBox changes

To learn WPF Command and CommandParameter I have a small WPF application with one TextBox and one Button. Whenever the button is pressed, ICommandTest should be called with the text of the text box as ...
Harald Coppoolse's user avatar
2 votes
1 answer
6k views

WPF command binding with input validation - how to enable the "save" button only if all input is valid

In my ViewModel I have implemented IDataErrorInfo interface (along with INotifyPropertyChanged). Input validation works as intended, I have no problems there. I have this property as part of ...
Saša Ćetković's user avatar
20 votes
2 answers
44k views

Multiple command parameters wpf button object

How can I send multiple parameters from Button in WPF? I am able to send single parameter which is value of TextBox properly. Here is the code. XAML <TextBox Grid.Row="1" Height="23" ...
Mandar Jogalekar's user avatar
9 votes
2 answers
18k views

How to bind a Command to double-click on a row in DataGrid

I have developed a WPF UserControl that is intended to be used as a pick list as follows: A DataGrid bound to a CollectionView of entities (e.g. of Employees) A TextBox above the DataGrid that can be ...
Joe's user avatar
  • 123k
9 votes
2 answers
6k views

WPF/MVVM: Disable a Button's state when the ViewModel behind the UserControl is not yet Initialized?

I have a DocumentListView.Xaml with a ListBox and 3 Buttons. Behind that UserControl sits a DocumentListViewModel with 3 Buttons and their Command Property bound to 3 RelayCommands. I have 3 ...
Elisabeth's user avatar
  • 20.9k
7 votes
2 answers
3k views

Are there any performance implications with CanExecuteCommand?

What are the performance implications of using the CanExecuteCommand of the ICommand object. Is the method executed over and over again? I need to iterate through a collection of about 200 objects ...
ganeshran's user avatar
  • 3,512
5 votes
2 answers
2k views

Is there a way to desaturate an Image on a Button thats disabled?

Is there a way I can desaturate images in Buttons that are disabled? eg. ICommand.CanExecute = false? or do I need to use separate images + Style/Triggers
Jiew Meng's user avatar
  • 85.8k
5 votes
2 answers
6k views

WPF Custom ICommand implementation and the CanExecuteChanged event

in my WPF UI, I use RoutedCommands that I refer to in my xaml via the following code: Command="viewModel:MessageListViewModel.DeleteMessagesCommand" I don't like this static link to my ViewModel ...
Sebastian Edelmeier's user avatar
4 votes
2 answers
7k views

ICommand CanExecuteChanged not updating

I am trying for MVVM pattern basic level and got struck at ICommand CanExecute changed. I have XAML binding as follows: <ListBox ItemsSource="{Binding Contact.Addresses}" x:Name="...
surpavan's user avatar
  • 1,392
3 votes
2 answers
980 views

Why use commands in MVVM

I'm actually learning MVVM pattern. I can't understand one thing about commands. Why use them? Why don't just use a function (in the View) which call a ViewModel's function? What commands provide us?...
Zwo's user avatar
  • 1,103
2 votes
1 answer
186 views

Why Are ICommand Properties Treated Specially by Bindings?

So far, I had the impression that WPF generally looks at the actual type of an object it gets via a binding or in any other way to determine what templates, styles and representation to use. However, ...
O. R. Mapper's user avatar
  • 20.4k
2 votes
3 answers
3k views

Another implementation of WPF Event to Command (with problems)

I am looking at various implementations of hooking a ICommand up to a control's event. So for instance the GotFocus of a TextBox should call a GotFocusCommand in my View Model. I then got an idea to ...
Oliver's user avatar
  • 113
1 vote
1 answer
5k views

ICommand method execute parameter value

I try to understand the ICommand from wpf. In my Event class I implement the ICommand and their methods. one method is the Execute: public void Execute(object parameter) { //Do something } now ...
David's user avatar
  • 4,047
0 votes
1 answer
4k views

WPF ListBox Commands within ListBoxItem MVVM Command Binding

I have been trying to find a way to have command buttons within my ListBoxItem's of a ListBox control. I use MVVM and Command Binding to the DataContext via ICommand interface. I am unable to find a ...
Edd's user avatar
  • 701
56 votes
1 answer
3k views

Unit-Test project fails to build after installing .NET 4.5

I have a WPF solution in .NET 4.0 that includes a Unit Test project which tests the different commands used in the ViewModels. Everything was working fine, but then I installed .NET 4.5 and VS2012. ...
atiyar's user avatar
  • 8,042
21 votes
2 answers
6k views

What replaces CommandManager in WinRT?

I'm getting started with Metro style applications (I know we're not supposed to call it Metro, but I can never remember what it's supposed to be called...), and I'm implementing a DelegateCommand ...
Thomas Levesque's user avatar
17 votes
4 answers
35k views

How to cancel window closing in MVVM WPF application

How can I cancel exiting from particular form after Cancel button (or X at the top right corner, or Esc) was clicked? WPF: <Window ... x:Class="MyApp.MyView" ... /> <Button Content="...
Andrii Muzychuk's user avatar
14 votes
3 answers
3k views

Translation of C# ActionCommand:ICommand into VB.net

I found a C# class ActionCommand, that implements ICommand and bases on delegates for Execute and CanExecute. Looks perfect for me so far. public class ActionCommand : ICommand { private ...
Markus's user avatar
  • 4,557
13 votes
2 answers
7k views

Bind a button to a command (Windows Phone 7.5)

I'm working on my windows-phone app which uses some simple data binding. I've already created a app which was based on the MvvM programming method.The app i'm currently working on also works by MvvM ...
Bart Teunissen's user avatar
11 votes
6 answers
18k views

WPF: TreeViewItem bound to an ICommand

I am busy creating my first MVVM application in WPF. Basically the problem I am having is that I have a TreeView (System.Windows.Controls.TreeView) which I have placed on my WPF Window, I have ...
Richard's user avatar
  • 3,307
10 votes
3 answers
6k views

ICommand.CanExecute being passed null even though CommandParameter is set

I have a tricky problem where I am binding a ContextMenu to a set of ICommand-derived objects, and setting the Command and CommandParameter properties on each MenuItem via a style: <ContextMenu ...
devios1's user avatar
  • 37.4k
9 votes
3 answers
2k views

Should I check an ICommand's CanExecute method before calling Execute from procedural code?

When using ICommands in XAML, WPF uses the CanExecute method to enable or disable controls associated with the command. But what if I am calling Execute from procedural code? Should I first check ...
Matthew's user avatar
  • 28.6k
7 votes
1 answer
9k views

Button Command CanExecute not called when property changed

I have a form with a textbox and a button. When that textbox has it's value changed, the button command doesn't call the CanExecute method of it's command. The command parameter is set but doesn't ...
Thiago Romam's user avatar
7 votes
2 answers
3k views

Saving a WPF canvas as an image following MVVM Pattern

I have a canvas, e.g. similar to this solution or many others using the ItemsControl. Now I want a button which should be bound to an ICommand. This command should call a method of ViewModel class ...
Rolfi's user avatar
  • 452
6 votes
3 answers
5k views

Updated title: Why ICommand.CanExecute is getting called all the time, instead of working like an event?

I am adopting MVVM pattern in WPF and have learned the use of Command. But in my implementation, the delegate I assigned to implement CanExecute is always called. I mean if I put a break point inside ...
tete's user avatar
  • 4,939
5 votes
3 answers
2k views

Button not enabled even after providing CommandParameter in WPF

I created a button whose commandparameter is set and command using a class that implements ICommand interface. But my button is disabled. Why is that? I got this code from here: ICommand is like a ...
Nikhil Agrawal's user avatar
5 votes
2 answers
21k views

WPF Interaction triggers in a Style to invoke commands on View Model [duplicate]

Possible Duplicate: How to add a Blend Behavior in a Style Setter when I use an interaction trigger in a style, I am getting the following error, 'triggers is not attachable element of type ...
skjagini's user avatar
  • 3,177
4 votes
1 answer
2k views

Can I call a command inside a command?

I have a closecommand defined inside my viewmodel for my dialog window. I have another command defined inside that viewmodel. Now I have that command binded to a control in my view. After ...
ecMode's user avatar
  • 540
4 votes
1 answer
2k views

Does MvvmCross work in Xamarin Studio?

I'm having a time getting a straight answer on this, I've done a lot of searches and reading, and tried a lot of stuff. It sometimes sounds possible and other times only sounds possible by doing ...
Bryan Weidman's user avatar
4 votes
1 answer
775 views

Is it possible to take advantage of the ICommand in an ASP.NET MVC project?

Basically, I am creating a portable class library, and creating a bunch of ViewModels: public class CustomerViewModel : ViewModelBase { public string FirstName { ... } // INPC public string ...
michael's user avatar
  • 15k
3 votes
1 answer
7k views

object sender is always null in RelayCommand

I am using RelayCommand to handle a button click, I need to get the sender parameter but it is always null, any idea why? ViewModel.cs private RelayCommand _expandClickCommand; public ...
Eamonn McEvoy's user avatar
3 votes
2 answers
8k views

Adding MouseBindings to Items in a databound WPF ListView

I'm trying to get a command in my ViewModel executed when the user clicks on a item in a ListView. When I add a ListViewItem in XAML I can just add a MouseBinding to its InputBindings. <ListView&...
Michael Menne's user avatar
3 votes
1 answer
470 views

Is it safe to call new RelayCommand (ICommand) in Expression-Bodied Properties

With expression bodied properties we can create a RelayCommand as follows public RelayCommand Command => _command ?? (_command = new RelayCommand(CommandExecute)); However this is possible also ...
TheGeneral's user avatar
  • 80.5k
3 votes
1 answer
2k views

WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?

I'm trying to determine the best way to cause ICommands' CanExecute() to be reflected in the UI. I understand that the Dispatcher is the WPF (engine?) that handles UI drawing, and that by default, ...
Justin Shidell's user avatar