relaycommand

C# WPF InvalidCastException in RelayCommand<T> [closed]

你说的曾经没有我的故事 提交于 2019-12-13 10:40:06
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 8 days ago . I would like to have a method like ShowImageCommand = new RelayCommand<Image, Audio>(ShowImageMethod); , but it's not possible, so I have created public class AudioAndImage { public Image Picture { get; set; } public Audio Sound { get; set; } } I have a new object of this type in ViewModel and set properties when

Does ICommand always requires an object as a parameter?

好久不见. 提交于 2019-12-13 06:19:42
问题 When I implement the ICommand interface, the following methods are created #region ICommand Members public bool CanExecute(object parameter) { } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { } #endregion The interesting part is public void Execute(object parameter) { } Simply because it indicates that it expects 1 parameter. What if I don't need to pass a parameter? In my ViewModel I have the following code public class DownloadViewModel : BaseViewModel

Pass different commandparameters to same command using RelayCommand WPF

心已入冬 提交于 2019-12-12 04:38:34
问题 So, what I am trying to achieve here is to use the same command to execute some different kind of code. The way I want to distinguish between the code I want to be executed can be done using commandparameters. I just don't see how I can do it the way I want to, when I have to use the RelayCommand. This means, that I have 2 different buttons, that both uses the same command, just with different commandparameters. This is my XAML so far: <RibbonButton SmallImageSource="../Images/whatever.png"

RelayCommand change canExecute automatic

夙愿已清 提交于 2019-12-11 11:23:44
问题 The current step of learning MVVM is RelayCommand for me. So i came up with this RelayCommand class: Relay Command class public class RelayCommand : ICommand { private readonly Action<object> _execute; private readonly Func<object, bool> _canExecute; public RelayCommand(Action<object> execute) : this(execute, null) { } public RelayCommand(Action<object> execute, Func<object, bool> canExecute) { _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute ??

How to bind a RelayCommand(MVVM) to a RoutedCommand? (CommandBinding)

放肆的年华 提交于 2019-12-11 08:15:10
问题 I want to create a custom class of a CommandBinding, where a RelayCommand of my ViewModel is executed when the RoutedCommand is executed. Currently there is only the possibility to create CommandBindings which have Executed methods in the codebehind class. Example: <CommandBinding Command="ApplicationCommands.Close" Executed="CloseCommandHandler" CanExecute="CanExecuteHandler"/> This needs the CloseCommandHandler methode in the code behind. I would want to write the following. <CommandBinding

object sender is always null in RelayCommand

不问归期 提交于 2019-12-11 02:58:04
问题 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 ICommand ExpandClickCommand { get { if (_expandClickCommand == null) { _expandClickCommand = new RelayCommand(ExpandClickCommandExecute, ExpandClickCommandCanExecute); } return _expandClickCommand; } } public void ExpandClickCommandExecute(object sender) { //sender is always null when i get here! } public bool

Binding CommandParameter on ItemsControl Tap event

瘦欲@ 提交于 2019-12-07 11:26:13
问题 I'm using an ItemsControl, and I want to identify which item was selected on the Tap command. My xaml is defined here: <ItemsControl ItemsSource="{Binding AllMyItems}"> <i:Interaction.Triggers> <i:EventTrigger EventName="Tap"> <cmd:EventToCommand Command="{Binding ItemSelectedCommand}" CommandParameter="{Binding}"/> </i:EventTrigger> </i:Interaction.Triggers> .... item template .... and here is my view model: public RelayCommand<MyItem> ItemSelectedCommand { get; private set; } public

.NET delegate equality?

不羁的心 提交于 2019-12-07 01:34:26
I think this is the question, anyway. I am using a RelayCommand, which decorates an ICommand with two delegates. One is Predicate for the _canExecute and the other is Action for the _execute method. ---Background motivation -- The motivation has to do with unit testing ViewModels for a WPF presentation. A frequent pattern is that I have one ViewModel that has an ObservableCollection, and I want a unit test to prove the data in that collection is what I expect given some source data (which also needs to be converted into a collection of ViewModels). Even though the data in both collections

How to bind application commands to view model(WPF)?

↘锁芯ラ 提交于 2019-12-07 00:31:48
问题 I have already read Josh Smiths article about binding commands to view model using RelayCommand. However I need to bind ApplicationCommands.Save to a view model so that when a user clicks the save menu item it is handled in the window. How is it possible? 回答1: The best solution I'm aware of us to use a service. For example, an ICommandBindingsProvider like this: public interface ICommandBindingsProvider { CommandBindingCollection CommandBindings { get; } } This gets injected into your

How to bind application commands to view model(WPF)?

↘锁芯ラ 提交于 2019-12-05 05:46:10
I have already read Josh Smiths article about binding commands to view model using RelayCommand. However I need to bind ApplicationCommands.Save to a view model so that when a user clicks the save menu item it is handled in the window. How is it possible? The best solution I'm aware of us to use a service. For example, an ICommandBindingsProvider like this: public interface ICommandBindingsProvider { CommandBindingCollection CommandBindings { get; } } This gets injected into your ViewModel and used like this: public MyViewModel(ICommandBindingsProvider commandBindings) { commandBindings.Add