icommand

WPF and MVVM. Binding Events

▼魔方 西西 提交于 2019-11-28 07:27:40
问题 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 capture its final position, to put in the database. But I can't bind events (MouseLetButtonDown, MouseLeftButtonUp and MouseMove) to commands. I've read about attached behaviours , but I need the arguments of the events (MouseButtonEventArgs and MouseEventArgs) to retrieve the position. Solution?

How to Bind a Command in WPF

梦想的初衷 提交于 2019-11-27 21:15:57
问题 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 interface and from the view model i create new instance of that class and binding works like a charm. This is the code that i used for command binding public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = this; testCommand = new MeCommand(processor); }

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

感情迁移 提交于 2019-11-27 18:15:00
It is very easy to bind Button s in WPF apps to Command s 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 when I hit Enter while the TextBox is focused. Currently, I'm using the following handler for the KeyUp event, but it looks ugly... and I can't put it in my VIEWMODEL class. private void TextBox_KeyUp(object sender, KeyEventArgs e) { if (e.Key == System.Windows.Input.Key.Enter) { // your event handler here e.Handled = true; MessageBox.Show("Enter Key is pressed!"); } } Is there a better way to do

Pass command parameter from the xaml

耗尽温柔 提交于 2019-11-27 17:22:30
问题 I try to do something like this: <DataGrid Name="myGrid" ItemSource="{Binding Path=MyCollection}"> <DataGrid.ContextMenu> <ContextMenu> <MenuItem Command="{Binding RemoveRow}" CommandParameter="{Binding ElementName=myGrid, Path=SelectedItem}"> </ContextMenu> </DataGridContextMenu> </DataGrid> but I got null always (I tried also SelectedIndex and SelectedValue) if I pass the following parameter to the execution code, it works: <MenuItem Command="{Binding RemoveRow}" CommandParameter="1"> 回答1:

CanExecuteChanged event of ICommand

北城余情 提交于 2019-11-27 14:59:40
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? The below explanation is on MSDN but I can’t understand it. CanExecuteChanged is raised if the command manager that centralizes the commanding operations detects a change in the command source that might invalidate a command that has been raised but not yet executed by the command binding. Can you please explain this in simple terms? Thanks...... CanExecuteChanged is raised when the CanExecute method

Pass command parameter to method in ViewModel in WPF?

寵の児 提交于 2019-11-27 08:03:52
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.Container.Child = objHistory; } if (sender == this.objMainWindow.btnNew_Item) { objMainWindow.Container.Child = objNewItem; } if (sender == this.objMainWindow.btnSide_Effects) { objMainWindow.Container.Child = objSideEffect; } } This is my meyhod in ViewModel that I want to pass CommandParameter . I use CommandParameter for button. Mark Feldman "ViewModel" implies MVVM. If you're doing MVVM you shouldn't be passing

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

假装没事ソ 提交于 2019-11-27 06:41:05
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 { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } I can't understand two things: what does the CanExecuteChanged event do? what does the CommandManager.RequerySuggested do? The above code is from the RelayCommand Class from here . CanExecuteChanged notifies any command sources (like a Button or MenuItem ) that are bound to that ICommand that the value returned by

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

孤街醉人 提交于 2019-11-27 06:01:10
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 system where I submit tasks to a background-thread based task processor. This submit happens on the WPF UI thread. When this submit happens, the object that manages my background thread does two things: It raises a "busy" event (still on the UI thread) that several view models respond to; when they receive this event, they set an IsEnabled flag on themselves to false . Controls in my views, which are

Is Josh Smith's implementation of the RelayCommand flawed?

雨燕双飞 提交于 2019-11-27 00:20:00
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 entire article for this question.) In general, I think the implementation is excellent, but I have a question about the delegation of CanExecuteChanged subscriptions to the CommandManager 's RequerySuggested event. The documentation for RequerySuggested states: Since this event is static, it will only hold onto the handler as a weak reference. Objects that listen for this event should keep a

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

馋奶兔 提交于 2019-11-26 22:40:26
问题 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 there a better namespace for doing this?) It's not a big deal to bind it, the big deal is to pass that PopulatingEventArgs argument to the bound command. So how do I do it according to the best practices of PRISM in particular and MVVM in general? 回答1: There is no built-in way, so here is how I do it