title: c# WPF help needed: How to show values from a object via WPF
body: <p>[Edited 2019-02-23; I hope it is correct to edit the question]</p>
  
  <p>I am complete newbie to WPF, and strugling.
  I have looked at alot of tutorials etc., but have not found the answer (in a way I understand).</p>
  
  <p>I am building an Yatzy game, and the Yatzy class it self is almost complete.
  Now I would like to add a GUI using WPF to the yatzy class.</p>
  
  <p>Thanks to the input from Reginald Blue answering v1 of my question.
  I am working on a view model. </p>
  
  <p>Mark Whitall have made a good <a href="https://www.markwithall.com/programming/2013/03/01/worlds-simplest-csharp-wpf-mvvm-example.html" rel="nofollow noreferrer">example</a>, but I cant migrate it to my needs:</p>
  
  <p>But it also troubles me. I hope that the code suplied are suffiecent, if not - ask :-)</p>
  
  <p>It looks like my view model, has "contact" to my model. Thats super
  But regarding my view connecting to my viewmodel, something seems to be wrong.</p>
  
  <p>What I would like to, as a start:
  1) When Fields GameStatus and GameName should have default values from my model. 
  Before making my viewmodel, that worked fine - but I have messed it up.
  I declare my thisYatzy object, in maincontrol.cs (in the viewmodel).
  Should I declare it at app.xaml level?
  2) When I click on "button" ShiftState, I would call my viewmodel to shift state, and update the view. But from the view i cant call the viewmodel?</p>
  
  <p>I hope that I have explained OK, and that someone of you can help :-)</p>
  
  <p><strong>VIEW:</strong></p>
  
  <p>app.xalm</p>
  
  <pre><code>&lt;Application x:Class="WPFYatzy.App"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:local="clr-namespace:WPFYatzy"
               StartupUri="Views/MainWindow.xaml"&gt;
      &lt;Application.Resources&gt;
  
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>app.xaml.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WPFYatzy.Views
  {
      /// &lt;summary&gt;
      /// Interaction logic for Page1.xaml
      /// &lt;/summary&gt;
      public partial class Page1 : Page
      {
          public Page1()
          {
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
                   }
      }
  }
  </code></pre>
  
  <p>mainwindow.xaml</p>
  
  <pre><code>&lt;Window x:Class="MinimalMVVM.Views.ConvertWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:Views="clr-namespace:WPFYatzy.Views"
          xmlns:ViewModels="clr-namespace:WPFYatzy.ViewModel"
          Title="YatzyGame - mainwindow"
          MinWidth="300"
          ResizeMode="NoResize"
          SizeToContent="WidthAndHeight"&gt;
  
      &lt;Window.DataContext&gt;
          &lt;ViewModels:MainControl/&gt;
      &lt;/Window.DataContext&gt;
  
      &lt;Views:Page1/&gt;
  
  &lt;/Window&gt;
  </code></pre>
  
  <p>mainwindow.xaml.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using YatzyGameCL;
  
  namespace WPFYatzy
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          //YatzyGame thisYatzy = new YatzyGame();
  
          public MainWindow()
          {
              //InitializeComponent();
          }
  
          private void OnInit(object sender, EventArgs e)
          {
  
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
  
          }
      }
  
  }
  </code></pre>
  
  <p>page1.xaml</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WPFYatzy.Views
  {
      /// &lt;summary&gt;
      /// Interaction logic for Page1.xaml
      /// &lt;/summary&gt;
      public partial class Page1 : Page
      {
          public Page1()
          {
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
                   }
      }
  }
  </code></pre>
  
  <p>page1.xaml.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WPFYatzy.Views
  {
      /// &lt;summary&gt;
      /// Interaction logic for Page1.xaml
      /// &lt;/summary&gt;
      public partial class Page1 : Page
      {
          public Page1()
          {
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
                   }
      }
  }
  </code></pre>
  
  <p><strong>Viewmodel</strong></p>
  
  <p>ObservableObject.cs</p>
  
  <pre><code>using System.ComponentModel;
  
  namespace WPFYatzy.ViewModel
  {
      public abstract class ObservableObject : INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
  
          protected void RaisePropertyChangedEvent(string propertyName)
          {
              var handler = PropertyChanged;
              if (handler != null)
                  handler(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p>DelegateCommand.cs</p>
  
  <pre><code>using System;
  using System.Windows.Input;
  
  namespace WPFYatzy.ViewModel
  {
      public class DelegateCommand : ICommand
      {
          private readonly Action _action;
  
          public DelegateCommand(Action action)
          {
              _action = action;
          }
  
          public void Execute(object parameter)
          {
              _action();
          }
  
          public bool CanExecute(object parameter)
          {
              return true;
          }
  
  #pragma warning disable 67
          public event EventHandler CanExecuteChanged { add { } remove { } }
  #pragma warning restore 67
      }
  }
  </code></pre>
  
  <p>MainControl.cs</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Windows.Input;
  using YatzyGameCL;
  
  namespace WPFYatzy.ViewModel
  {
      class MainControl : ObservableObject
      {
          private YatzyGame thisYatzy = new YatzyGame();
  
          private string _strGameName;
          private string _strGameStatus;
  
          public string GameName
          {
              get { return _strGameName; }
              set
              {
                  _strGameName = value;
                  RaisePropertyChangedEvent("GameName");
              }
  
          }
  
          public string GameStatus
          {
              get { return _strGameStatus; }
              set {
                  _strGameStatus = value;
                  RaisePropertyChangedEvent("GameStatus");
              }
          }
  
          public void ShiftState()
          {
              thisYatzy.GameStatus.NextAround();
  
              GameStatus = thisYatzy.GameStatus.ToString();
  
          }
      }
  }
  </code></pre>
  
  <p><strong>Model</strong>
  YatzyGameCL.DLL</p>
  
  <p>Methods to be used to make the framework "hang together" </p>
  
  <ul>
  <li>thisYatzy.GameStatus</li>
  <li>thisYatzy.GameName</li>
  </ul>