How to bind column header to property in ViewModel? (WPF MVVM)

Unfortunately, the column definitions of the DataGrid don’t inherit the DataContext, because they’re not part of the visual tree, so you can’t bind directly to the ViewModel. You need to resort to a workaround such as the one described in this article: <DataGrid.Resources> <local:BindingProxy x:Key=”proxy” Data=”{Binding}” /> </DataGrid.Resources> … <DataGridTextColumn Header=”{Binding Data.MyTitle, Source={StaticResource proxy}}”/>

Page Navigation using MVVM in Store App

There are 2 ways to do this, a simple way is to pass a relay command action from the view to the view model. public MainPage() { var vm = new MyViewModel(); vm.GotoPage2Command = new RelayCommand(()=>{ Frame.Navigate(typeof(Page2)) }); this.DataContext = vm; } <Button Command={Binding GoToPage2Command}>Go to Page 2</Button> Another way is by using an IocContainer … Read more

Binding Commands to Events?

Use System.Windows.Interactivity …xmlns:i=http://schemas.microsoft.com/expression/2010/interactivity… <Slider <i:Interaction.Triggers> <i:EventTrigger EventName=”ValueChanged”> <i:InvokeCommandAction Command=”{Binding MyCommand}” CommandParameter=”{Binding Text, ElementName=textBox}”/> </i:EventTrigger> </i:Interaction.Triggers> </Slider> Make sure your project references the assembly System.Windows.Interactivity. Source: MSDN Blog Executing a command from an event of your choice [Update] Have a look to to Microsoft.Xaml.Behaviors.Wpf (available since 03.12.2018) Official package by Microsoft.

MVVM – what is the ideal way for usercontrols to talk to each other

Typically, it’s best to try to reduce the amount of communication between parts, as each time two user controls “talk” to each other, you’re introducing a dependency between them. That being said, there are a couple of things to consider: UserControls can always “talk” to their containing control via exposing properties and using DataBinding. This … Read more

MVVM binding command to contextmenu item

(Edit) Since you mentioned this is in an ItemsControl’s template, things are different: 1) Get the BindingProxy class from this blog (and read the blog, as this is interesting information): How to bind to data when the DataContext is not inherited. Basically the elements in the ItemsControl (or ContextMenu) are not part of the visual … Read more

How to set focus to textbox using MVVM?

You can do this by adding a property to your ViewModel (or use an existing property) that indicates when the SetFocus should happen but the View should be responsible for actually setting the focus since that is purely View related. You can do this with a DataTrigger. View: <Grid Name=”LayoutRoot” DataContext=”{StaticResource MyViewModelInstance}”> <Grid.Style> <Style> <Style.Triggers> … Read more

mvvm how to make a list view auto scroll to a new Item in a list view

This solution is for a ListBox, but it could be modified for a ListView… This will scroll the selected item into view when you change the selected item from the ViewModel. Class: /// <summary> /// ListBoxItem Behavior class /// </summary> public static class ListBoxItemBehavior { #region IsBroughtIntoViewWhenSelected /// <summary> /// Gets the IsBroughtIntoViewWhenSelected value /// … Read more