title: C# WPF XAML Toolkit, switch views with button click
body: <p>I am using <a href="http://materialdesigninxaml.net/" rel="nofollow noreferrer">Material Design in XAML Toolkit</a>. I have the main window with drawer, which contains the list of user controls (app tabs). When I click on them - application tab switches between this controls. I want to add a button to the window, and when I click on it I want to switch between tabs too. You can see important parts of my code here:</p>
  
  <pre><code>    &lt;materialDesign:DialogHost Identifier="RootDialog" SnackbarMessageQueue="{Binding ElementName=MainSnackbar, Path=MessageQueue}"&gt;
      &lt;materialDesign:DrawerHost IsLeftDrawerOpen="{Binding ElementName=MenuToggleButton, Path=IsChecked}"&gt;
          &lt;materialDesign:DrawerHost.LeftDrawerContent&gt;
              &lt;DockPanel MinWidth="212"&gt;
                  &lt;ToggleButton Style="{StaticResource MaterialDesignHamburgerToggleButton}" 
                                  DockPanel.Dock="Top"
                                  HorizontalAlignment="Right" Margin="16"
                                  IsChecked="{Binding ElementName=MenuToggleButton, Path=IsChecked, Mode=TwoWay}" /&gt;
                  &lt;ListBox x:Name="DemoItemsListBox" Margin="0 16 0 16" SelectedIndex="0"                         
                           ItemsSource="{Binding DemoItems}"
                           PreviewMouseLeftButtonUp="UIElement_OnPreviewMouseLeftButtonUp"&gt;
                      &lt;ListBox.ItemTemplate&gt;
                          &lt;DataTemplate DataType="helpers:DemoItem"&gt;
                              &lt;TextBlock Text="{Binding Name}" Margin="32 0 32 0" /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ListBox.ItemTemplate&gt;
                  &lt;/ListBox&gt;
              &lt;/DockPanel&gt;
          &lt;/materialDesign:DrawerHost.LeftDrawerContent&gt;
          &lt;DockPanel&gt;
              &lt;materialDesign:ColorZone Padding="16" materialDesign:ShadowAssist.ShadowDepth="Depth2"
                                          Mode="PrimaryDark" DockPanel.Dock="Top"&gt;
                  &lt;DockPanel&gt;
                      &lt;ToggleButton Style="{StaticResource MaterialDesignHamburgerToggleButton}" IsChecked="False"
                                      x:Name="MenuToggleButton"/&gt;
  
                      &lt;materialDesign:PopupBox x:Name="popupBox"&gt;
                          &lt;TextBlock&gt;Check me please&lt;/TextBlock&gt;
                      &lt;/materialDesign:PopupBox&gt;
                      &lt;CheckBox x:Name="LizenzeCheckBox" DockPanel.Dock="Right" Style="{StaticResource MaterialDesignCheckBox}" Tag="False"&gt;
                          &lt;CheckBox.IsChecked&gt;
                              &lt;Binding Path="Tag" RelativeSource="{RelativeSource Self}"&gt;
                                  &lt;Binding.ValidationRules&gt;
                                      &lt;helpers:IsCheckedValidationRule /&gt;
                                  &lt;/Binding.ValidationRules&gt;
                              &lt;/Binding&gt;
                          &lt;/CheckBox.IsChecked&gt;CheckBox text&lt;/CheckBox&gt;
                      &lt;TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22"&gt;My App&lt;/TextBlock&gt;
                  &lt;/DockPanel&gt;
              &lt;/materialDesign:ColorZone&gt;
              &lt;Button x:Name="TheBUTTON" Click="Button_Click"&gt;Ckicc&lt;/Button&gt;
              &lt;Grid&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="Auto" /&gt;
                      &lt;RowDefinition Height="*" /&gt;
                  &lt;/Grid.RowDefinitions&gt;
  
                  &lt;ScrollViewer Grid.Row="1" 
                                HorizontalScrollBarVisibility="{Binding ElementName=DemoItemsListBox, Path=SelectedItem.HorizontalScrollBarVisibilityRequirement}"
                                VerticalScrollBarVisibility="{Binding ElementName=DemoItemsListBox, Path=SelectedItem.VerticalScrollBarVisibilityRequirement}" 
                                Padding="{Binding ElementName=DemoItemsListBox, Path=SelectedItem.MarginRequirement}"&gt;
                      &lt;ContentControl Content="{Binding ElementName=DemoItemsListBox, Path=SelectedItem.Content}" /&gt;
                  &lt;/ScrollViewer&gt;&lt;/Grid&gt;
  </code></pre>
  
  <p>This is my main window xaml code, as you can see, I bind ListBox Values to DemoItem[] array from viewModel. "TheButton" onclick event is the event which I want to use for tab switching.</p>
  
  <p>My main window view model is:</p>
  
  <pre><code>public class MainWindowViewModel
  {
      public DemoItem[] DemoItems { get; }
  
      public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
      {
          if (snackbarMessageQueue == null) throw new ArgumentNullException(nameof(snackbarMessageQueue));
          DemoItems = new[]
          {
              new DemoItem("Tab1", new Tab1()),
              new DemoItem("Tab2", new Tab2()),
              new DemoItem("Tab3", new Tab3()),
          };
      }
  }
  </code></pre>
  
  <p>The MainWindow.cs is:</p>
  
  <pre><code> public partial class MainWindow : Window
  {
      public static Snackbar Snackbar;
  
      public MainWindow()
      {
          InitializeComponent();
  
          Task.Factory.StartNew(() =&gt;
          {
              Thread.Sleep(1000);
          }).ContinueWith(t =&gt;
          {
              MainSnackbar.MessageQueue.Enqueue("Welcome to my app");
          }, TaskScheduler.FromCurrentSynchronizationContext());
  
          DataContext = new MainWindowViewModel(MainSnackbar.MessageQueue);
  
      }
  
      private void UIElement_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
      {
          //until we had a StaysOpen glag to Drawer, this will help with scroll bars
          var dependencyObject = Mouse.Captured as DependencyObject;
          while (dependencyObject != null)
          {
              if (dependencyObject is ScrollBar) return;
              dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
          }
          MenuToggleButton.IsChecked = false;
      } 
  
      private void Button_Click(object sender, RoutedEventArgs e)
      {
        //what to do here?
      }
  }
  </code></pre>
  
  <p>The DemoItem Class is: </p>
  
  <pre><code>public class DemoItem : INotifyPropertyChanged
  {
      private string _name;
      private object _content;
      private ScrollBarVisibility _horizontalScrollBarVisibilityRequirement;
      private ScrollBarVisibility _verticalScrollBarVisibilityRequirement;
      private Thickness _marginRequirement = new Thickness(16);
  
      public DemoItem(string name, object content)
      {
          _name = name;
          Content = content;
      }
  
      public string Name
      {
          get { return _name; }
          set { this.MutateVerbose(ref _name, value, RaisePropertyChanged()); }
      }
  
      public object Content
      {
          get { return _content; }
          set { this.MutateVerbose(ref _content, value, RaisePropertyChanged()); }
      }
  
      public ScrollBarVisibility HorizontalScrollBarVisibilityRequirement
      {
          get { return _horizontalScrollBarVisibilityRequirement; }
          set { this.MutateVerbose(ref _horizontalScrollBarVisibilityRequirement, value, RaisePropertyChanged()); }
      }
  
      public ScrollBarVisibility VerticalScrollBarVisibilityRequirement
      {
          get { return _verticalScrollBarVisibilityRequirement; }
          set { this.MutateVerbose(ref _verticalScrollBarVisibilityRequirement, value, RaisePropertyChanged()); }
      }
  
      public Thickness MarginRequirement
      {
          get { return _marginRequirement; }
          set { this.MutateVerbose(ref _marginRequirement, value, RaisePropertyChanged()); }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      private Action&lt;PropertyChangedEventArgs&gt; RaisePropertyChanged()
      {
          return args =&gt; PropertyChanged?.Invoke(this, args);
      }
  }
  </code></pre>
  
  <p>My MutateVerbose function looks like:</p>
  
  <pre><code>public static void MutateVerbose&lt;TField&gt;(this INotifyPropertyChanged instance, ref TField field, TField newValue, Action&lt;PropertyChangedEventArgs&gt; raise, [CallerMemberName] string propertyName = null)
      {
          if (EqualityComparer&lt;TField&gt;.Default.Equals(field, newValue)) return;
          field = newValue;
          raise?.Invoke(new PropertyChangedEventArgs(propertyName));
      }
  </code></pre>
  
  <p>I don't know how to switch tabs with button click in this situation. Help me, please! </p>