WPF Binding UI events to commands in ViewModel

You should use an EventTrigger in combination with InvokeCommandAction from the Windows.Interactivity namespace. Here is an example: <ListBox …> <i:Interaction.Triggers> <i:EventTrigger EventName=”SelectionChanged”> <i:InvokeCommandAction Command=”{Binding SelectedItemChangedCommand}”/> </i:EventTrigger> </i:Interaction.Triggers> </ListBox> You can reference System.Windows.Interactivity by going Add reference > Assemblies > Extensions. And the full i namespace is: xmlns:i=”clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity”.

How to bind WPF button to a command in ViewModelBase?

<Grid > <Grid.ColumnDefinitions> <ColumnDefinition Width=”*”/> </Grid.ColumnDefinitions> <Button Command=”{Binding ClickCommand}” Width=”100″ Height=”100″ Content=”wefwfwef”/> </Grid> the code behind for the window: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = new ViewModelBase(); } } The ViewModel: public class ViewModelBase { private ICommand _clickCommand; public ICommand ClickCommand { get { return _clickCommand ?? (_clickCommand … Read more

Handling Dialogs in WPF with MVVM

I suggest forgoing the 1990’s modal dialogs and instead implementing a control as an overlay (canvas+absolute positioning) with visibility tied to a boolean back in the VM. Closer to an ajax type control. This is very useful: <BooleanToVisibilityConverter x:Key=”booltoVis” /> as in: <my:ErrorControl Visibility=”{Binding Path=ThereWasAnError, Mode=TwoWay, Converter={StaticResource booltoVis}, UpdateSourceTrigger=PropertyChanged}”/> Here’s how I have one implemented … Read more

Why RelayCommand

Commands are used to separate the semantics and the object that invokes a command from the logic that executes the command i.e. it separates UI component from the logic that needs to be executed on command invocation. So, that you can test business logic separately using test cases and also your UI code is loosely … Read more

What is the difference between MVC and MVVM? [closed]

MVC/MVVM is not an either/or choice. The two patterns crop up, in different ways, in both ASP.Net and Silverlight/WPF development. For ASP.Net, MVVM is used to two-way bind data within views. This is usually a client-side implementation (e.g. using Knockout.js). MVC on the other hand is a way of separating concerns on the server-side. For … Read more

ICommand MVVM implementation

This is almost identical to how Karl Shifflet demonstrated a RelayCommand, where Execute fires a predetermined Action<T>. A top-notch solution, if you ask me. public class RelayCommand : ICommand { private readonly Predicate<object> _canExecute; private readonly Action<object> _execute; public RelayCommand(Predicate<object> canExecute, Action<object> execute) { _canExecute = canExecute; _execute = execute; } public event EventHandler CanExecuteChanged … Read more

Pushing read-only GUI properties back into ViewModel

Yes, I’ve done this in the past with the ActualWidth and ActualHeight properties, both of which are read-only. I created an attached behavior that has ObservedWidth and ObservedHeight attached properties. It also has an Observe property that is used to do the initial hook-up. Usage looks like this: <UserControl … SizeObserver.Observe=”True” SizeObserver.ObservedWidth=”{Binding Width, Mode=OneWayToSource}” SizeObserver.ObservedHeight=”{Binding … Read more

How to bind to a PasswordBox in MVVM

Maybe I am missing something, but it seems like most of these solutions overcomplicate things and do away with secure practices. This method does not violate the MVVM pattern and maintains complete security. Yes, technically it is code behind, but it is nothing more than a “special case” binding. The ViewModel still has no knowledge … Read more

INotifyPropertyChanged vs. DependencyProperty in ViewModel

Kent wrote an interesting blog about this topic: View Models: POCOs versus DependencyObjects. Short summary: DependencyObjects are not marked as serializable The DependencyObject class overrides and seals the Equals() and GetHashCode() methods A DependencyObject has thread affinity – it can only be accessed on the thread on which it was created I prefer the POCO … Read more

Add directives from directive in AngularJS

In cases where you have multiple directives on a single DOM element and where the order in which they’re applied matters, you can use the priority property to order their application. Higher numbers run first. The default priority is 0 if you don’t specify one. EDIT: after the discussion, here’s the complete working solution. The … Read more