MVVM pattern violation: MediaElement.Play()

1) Do not call Play() from the view model. Raise an event in the view model instead (for instance PlayRequested) and listen to this event in the view: view model: public event EventHandler PlayRequested; … if (this.PlayRequested != null) { this.PlayRequested(this, EventArgs.Empty); } view: ViewModel vm = new ViewModel(); this.DataContext = vm; vm.PlayRequested += (sender, … Read more

Keybinding a RelayCommand

I don’t think you can do this from XAML, for exactly the reasons you describe. I ended up doing it in the code-behind. Although it’s code, it’s only a single line of code, and still rather declarative, so I can live with it. However, I’d really hope that this is solved in the next version … Read more

WPF Context menu on left click

Here is a XAML only solution. Just add this style to your button. This will cause the context menu to open on both left and right click. Enjoy! <Button Content=”Open Context Menu”> <Button.Style> <Style TargetType=”{x:Type Button}”> <Style.Triggers> <EventTrigger RoutedEvent=”Click”> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty=”ContextMenu.IsOpen”> <DiscreteBooleanKeyFrame KeyTime=”0:0:0″ Value=”True”/> </BooleanAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Style.Triggers> <Setter Property=”ContextMenu”> … Read more

WPF loading spinner

A very simple “plug and play” spinner could be one of the spinning icons from the Font Awesome Wpf Package (Spinning icons). The usage is quite simple, just install the nuget package: PM> Install-Package FontAwesome.WPF Then add the reference to the namespace xmlns:fa=”http://schemas.fontawesome.io/icons/” and use the ImageAwesome control. Set the Spin=”True” property and select one … Read more

Is MVVM pattern broken?

What do you think, have I broken the MVVM-Pattern? No. The view model have no dependency upon the view, it only knows about an interface that you could easily mock in your unit tests. So this doesn’t really break the pattern as long as “View” is just an abstraction of something. For type-safety reasons you … Read more

WPF MVVM: Convention over Configuration for ResourceDictionary?

Rather than writing code to explicitly add things to the ResourceDictionary, how about just generating the right view on demand? You can do this with a ValueConverter. Your resources would look like this: <Views:ConventionOverConfigurationConverter x:Key=”MyConverter”/> <DataTemplate DataType=”{x:Type ViewModels:ViewModelBase}”> <ContentControl Content=”{Binding Converter={StaticResource MyConverter}}”/> </DataTemplate> You still need a DataTemplate resource, but as long as your ViewModels … Read more