icommand

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

天涯浪子 提交于 2019-11-30 03:38:33
问题 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 used to filter items displayed in the DataGrid. I want to expose a Command that will be executed when the user double-clicks on a row in the DataGrid. The container can then react to this by doing something with the SelectedItem in the DataGrid. So far I've tried to handle the double-click as

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

元气小坏坏 提交于 2019-11-29 13:43:49
问题 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 Controller like AdministrationController, BillingController, ReportController. Every Controller has ObservableCollections like Customer 1 : N Order 1: N Document same for the other Controller. In one Controller I have a special binding situation. When my DocumentListViewModel is not initialized by its

WPF and MVVM. Binding Events

删除回忆录丶 提交于 2019-11-29 13:37:09
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? Rick Sladkey When writing an MVVM graphical application, it is tempting to try to send all the events

Multiple command parameters wpf button object

↘锁芯ラ 提交于 2019-11-29 11:17:43
问题 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" HorizontalAlignment="Left" Margin="133,22,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" /> <Button Content="Button" Grid.Row="1" Height="23" Command="{Binding Path=CommandClick}" CommandParameter="{Binding Text,ElementName=textBox1}" HorizontalAlignment="Left" Margin="133,62,0,0" Name="button1"

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

删除回忆录丶 提交于 2019-11-29 06:22:44
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" Command="{Binding SaveCommand}" /> Where obviously the SaveCommand in my view model is effectively going to

Pass command parameter from the xaml

 ̄綄美尐妖づ 提交于 2019-11-29 03:05:20
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"> Try something like this in your CommandParameter, <DataGrid.ContextMenu> <ContextMenu> <MenuItem Header=

How to Bind a Command in WPF

不问归期 提交于 2019-11-28 23:31:01
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); } ICommand testCommand; public ICommand test { get { return testCommand; } } public void processor() {

WPF ICommand vs RoutedCommand

僤鯓⒐⒋嵵緔 提交于 2019-11-28 16:04:43
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 I need to iplement an ICommand ? What about MVVM model? Which one suits better for this purpose? Richard McGuire As you have noticed the RoutedCommand class is an implementation of the ICommand interface, its main distinction if that its function is similar to that of a RoutedEvent : The Execute and CanExecute methods on a RoutedCommand do not contain the application logic for the command as is

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

时光总嘲笑我的痴心妄想 提交于 2019-11-28 11:08:40
问题 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 IDataErrorInfo public string Error { get { return this[null]; } } To my understanding, Error should be empty if all validated inputs pass validation, so I pass this as my CanExecute method return !string.IsNullOrEmpty(Error); But, my "save" button never gets enabled. My gues is that CanExecuteChanged

WPF ViewModel Commands CanExecute issue

蹲街弑〆低调 提交于 2019-11-28 09:12:30
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 resources of the View (MainWindow), and using a CommandReference from the MVVMToolkit to access the current DataContext (ViewModel) Commands. When I debug the application, it appears that the CanExecute method on the command is not being called except at the creation of the window, therefore my Context MenuItems are not being enabled or disabled as I would have expected. I've cooked up a simple sample (