icommand

How to cancel window closing in MVVM WPF application

痞子三分冷 提交于 2019-12-05 00:43:25
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="Cancel" Command="{Binding CancelCommand}" IsCancel="True"/> </Window> ViewModel: public class MyViewModel : Screen { private CancelCommand cancelCommand; public CancelCommand CancelCommand { get { return cancelCommand; } } public MyViewModel() { cancelCommand = new CancelCommand(this); } } public class CancelCommand : ICommand { public CancelCommand(MyViewModel viewModel) { this.viewModel = viewModel; } public

ICommand.CanExecute being passed null even though CommandParameter is set

半世苍凉 提交于 2019-12-04 21:16:25
问题 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 ItemsSource="{Binding Source={x:Static OrangeNote:Note.MultiCommands}}"> <ContextMenu.Resources> <Style TargetType="MenuItem"> <Setter Property="Header" Value="{Binding Path=Title}" /> <Setter Property="Command" Value="{Binding}" /> <Setter Property="CommandParameter" Value="{Binding Source={x:Static

Translation of C# ActionCommand:ICommand into VB.net

一个人想着一个人 提交于 2019-12-04 16:58:19
问题 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 readonly Action<object> _executeHandler; private readonly Func<object, bool> _canExecuteHandler; public ActionCommand(Action<object> execute, Func<object, bool> canExecute) { if (execute == null) throw new ArgumentNullException("Execute cannot be null"); _executeHandler = execute; _canExecuteHandler =

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

怎甘沉沦 提交于 2019-12-04 13:20:27
问题 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 the delegate function, it shows that this function keeps getting called. To my understanding (and a natural way of thinking, but of course I can be wrong), this delegate only gets called when I somehow notifies the change of the state and that's when the CommandManager (re)checks the CanExecute

Button Command CanExecute not called when property changed

為{幸葍}努か 提交于 2019-12-04 08:59:38
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 seem to change. After load the window, the button remains disabled. <TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> <Button Content="Save" Command="{Binding SaveChangesCommand}" CommandParameter="{Binding Name}" /> I know the binding is working because I created a behavior that receives a target binding and raise the CanExecute when the the binding changes. With

Saving a WPF canvas as an image following MVVM Pattern

元气小坏坏 提交于 2019-12-04 06:34:25
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 which can save the image. The saving method is clear, but how do I do the binding following the MVVM pattern? You could pass the Canvas to the ViewModel's Save method using a CommandParameter <Button Content="Save" Command="{Binding SaveCanvasCommand}" CommandParameter="{Binding ElenementName=myCanvas}" ?> <Canvas x:Name="myCanvas"> <!-- Stuff to save --> </Canvas> And somewhere in you ViewModel or

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

偶尔善良 提交于 2019-12-03 18:01:46
问题 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 using a command instead of an event handler? If I have <Button x:Name="SaveButton" Content="Save" Click="SaveButton_Click" /> ... private void SaveButton_Click(object sender, RoutedEventArgs e) { _myViewModel.SaveChanges(); } Then why is this prefered? <Button x:Name="SaveButton" Content="Save"

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

China☆狼群 提交于 2019-12-03 15:51:12
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 the delegate function, it shows that this function keeps getting called. To my understanding (and a natural way of thinking, but of course I can be wrong), this delegate only gets called when I somehow notifies the change of the state and that's when the CommandManager (re)checks the CanExecute property and modify the IsEnabled property of the UI element. Here is my implementation of VB.NET,

ICommand.CanExecute being passed null even though CommandParameter is set

穿精又带淫゛_ 提交于 2019-12-03 13:51:49
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 ItemsSource="{Binding Source={x:Static OrangeNote:Note.MultiCommands}}"> <ContextMenu.Resources> <Style TargetType="MenuItem"> <Setter Property="Header" Value="{Binding Path=Title}" /> <Setter Property="Command" Value="{Binding}" /> <Setter Property="CommandParameter" Value="{Binding Source={x:Static OrangeNote:App.Screen}, Path=SelectedNotes}" /> ... However, while ICommand.Execute( object ) gets

Bind a button to a command (Windows Phone 7.5)

▼魔方 西西 提交于 2019-12-03 12:55:28
问题 I'm working on my windows-phone app wich uses some simple data binding. I've allready created a app wich was based on the MvvM programming method.The app i'm curently working on also works by MvvM method. Because i want to keep my code behind as clean as possible i was looking for a way to make the "button click event" (wich normaly takes place in the codebehind page) take place in my viewmodel or mainviewmodel. I have searched the internet in need of a simple explanation for the Icommand