title: WPF bind label content to variable
body: <p>I have created a simple Tic Tac Toe application to learn WPF. But I can't manage to bind the o_win_counter to my o_win_count label. How to achieve this? Later on I also want to bind the other counter labels to variables.</p>
  
  <p>App.xaml</p>
  
  <pre><code>&lt;Application x:Class="FirstApplication.App"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           StartupUri="MainWindow.xaml"&gt;
  &lt;Application.Resources&gt;
          &lt;ResourceDictionary Source="ResourceDictionary.xaml" /&gt;
  &lt;/Application.Resources&gt;
  </code></pre>
  
  <p></p>
  
  <p>ResourceDictionary.xaml</p>
  
  <pre><code>&lt;ResourceDictionary 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  
  &lt;Style TargetType="Button"&gt;
      &lt;Setter Property="BorderThickness" Value="1" /&gt;
      &lt;Setter Property="Foreground" Value="#FF959595" /&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Center" /&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Center" /&gt;
      &lt;Setter Property="Padding" Value="10,0" /&gt;
      &lt;Setter Property="Margin" Value="2" /&gt;
      &lt;Setter Property="FontFamily" Value="Segoe UI" /&gt;
      &lt;Setter Property="Height" Value="25" /&gt;
      &lt;Setter Property="MinWidth" Value="100" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                  &lt;Grid&gt;
                      &lt;VisualStateManager.VisualStateGroups&gt;
                          &lt;VisualStateGroup x:Name="CommonStates"&gt;
                              &lt;VisualState x:Name="Normal"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ColorAnimation To="#FFFFFFFF" Storyboard.TargetName="BgBrush"
                                                  Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
                                                  Duration="0:0:0.07" /&gt;
                                      &lt;ColorAnimation To="#FFDEDEDE" Storyboard.TargetName="BgBrush"
                                                  Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                  Duration="0:0:0.07" /&gt;
                                      &lt;ColorAnimation To="#FF959595" Storyboard.TargetName="BrBrush"
                                                  Storyboard.TargetProperty="Color" Duration="0:0:0.07" /&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                              &lt;VisualState x:Name="MouseOver"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ColorAnimation To="#FF00B4E4" Storyboard.TargetName="BgBrush"
                                                  Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
                                                  Duration="0:0:0.07" /&gt;
                                      &lt;ColorAnimation To="#FF0083C3" Storyboard.TargetName="BgBrush"
                                                  Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                  Duration="0:0:0.07" /&gt;
                                      &lt;ColorAnimation To="#FF4C7B8F" Storyboard.TargetName="BrBrush"
                                                  Storyboard.TargetProperty="Color" Duration="0:0:0.07" /&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                              &lt;VisualState x:Name="Pressed"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ColorAnimation To="#DBEDFD" Storyboard.TargetName="BgBrush"
                                                  Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
                                                  Duration="0:0:0.05" /&gt;
                                      &lt;ColorAnimation To="#C4E0FC" Storyboard.TargetName="BgBrush"
                                                  Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                  Duration="0:0:0.05" /&gt;
                                      &lt;ColorAnimation To="#4C7B8F" Storyboard.TargetName="BrBrush"
                                                  Storyboard.TargetProperty="Color" Duration="0:0:0.05" /&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                              &lt;VisualState x:Name="Disabled"&gt;
                                  &lt;Storyboard&gt;
                                      &lt;ColorAnimation To="#EB2828" Storyboard.TargetName="BgBrush"
                                                  Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
                                                  Duration="0:0:0" /&gt;
                                      &lt;ColorAnimation To="#EB2828" Storyboard.TargetName="BgBrush"
                                                  Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                  Duration="0:0:0" /&gt;
                                      &lt;ColorAnimation To="#D9D9D9" Storyboard.TargetName="BrBrush"
                                                  Storyboard.TargetProperty="Color" Duration="0:0:0" /&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/VisualState&gt;
                          &lt;/VisualStateGroup&gt;
                      &lt;/VisualStateManager.VisualStateGroups&gt;
                      &lt;Border x:Name="Chrome" BorderThickness="{TemplateBinding BorderThickness}"
                          SnapsToDevicePixels="true"&gt;
                          &lt;Border.BorderBrush&gt;
                              &lt;SolidColorBrush x:Name="BrBrush" Color="#ACACAC" /&gt;
                          &lt;/Border.BorderBrush&gt;
                          &lt;Border.Background&gt;
                              &lt;LinearGradientBrush x:Name="BgBrush" EndPoint="0,1" StartPoint="0,0"&gt;
                                  &lt;GradientStop Color="#FFF0F0F0" Offset="0" /&gt;
                                  &lt;GradientStop Color="#FFE5E5E5" Offset="1" /&gt;
                              &lt;/LinearGradientBrush&gt;
                          &lt;/Border.Background&gt;
                          &lt;ContentPresenter
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"
                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /&gt;
                      &lt;/Border&gt;
                  &lt;/Grid&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsMouseOver" Value="true"&gt;
                          &lt;Setter Property="Foreground" Value="#FFFFFF" /&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsEnabled" Value="false"&gt;
                          &lt;Setter Property="Foreground" Value="#ADADAD" /&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsPressed" Value="true"&gt;
                          &lt;Setter Property="Foreground" Value="#000000" /&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p></p>
  
  <p>MainWindow.xaml.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  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 FirstApplication
  {
  /// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class MainWindow : Window, INotifyPropertyChanged
  {
  public bool turn = true;
  public int turn_count = 0;
  private int _o_win_counter;
  
  public MainWindow()
  {
      InitializeComponent();
  }
  
  public int o_win_counter
  {
      get
      {
          return _o_win_counter;
      }
      set
      {
          if (_o_win_counter != value)
          {
              _o_win_counter = value;
              OnPropertyChanged("o_win_counter");
          }
      }
  }
  
  public event PropertyChangedEventHandler PropertyChanged;
  
  protected void OnPropertyChanged(string propertyName)
  {
      if (PropertyChanged != null)
      {
          PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
  }
  
  private void button_click(object sender, RoutedEventArgs e)
  {
      Button button = (Button)sender;
      if (turn)
          button.Content = "X";
      else
      {
          button.Content = "O";
      }
  
      turn = !turn;
      button.IsEnabled = false;
      turn_count++;
  
      checkForWinner();
  }
  
  private void checkForWinner()
  {
      bool isWinner = false;
  
      //horizontal
      if (A.Content == B.Content &amp;&amp; (B.Content == C.Content) &amp;&amp; (!A.IsEnabled))
          isWinner = true;
      else if (D.Content == E.Content &amp;&amp; (D.Content == F.Content) &amp;&amp; (!D.IsEnabled))
          isWinner = true;
      else if (G.Content == H.Content &amp;&amp; (G.Content == I.Content) &amp;&amp; (!G.IsEnabled))
          isWinner = true;
  
      //vertical
      else if (A.Content == D.Content &amp;&amp; (D.Content == G.Content) &amp;&amp; (!A.IsEnabled))
          isWinner = true;
      else if (B.Content == E.Content &amp;&amp; (B.Content == H.Content) &amp;&amp; (!B.IsEnabled))
          isWinner = true;
      else if (C.Content == F.Content &amp;&amp; (C.Content == I.Content) &amp;&amp; (!C.IsEnabled))
          isWinner = true;
  
      //diagonal
      else if (A.Content == E.Content &amp;&amp; (E.Content == I.Content) &amp;&amp; (!A.IsEnabled))
          isWinner = true;
      else if (G.Content == E.Content &amp;&amp; (E.Content == C.Content) &amp;&amp; (!G.IsEnabled))
          isWinner = true;
  
  
      if (isWinner)
      {
          disableButtons();
  
          String winner = "";
          if (turn)
          {
              winner = "O";
          }
          else
          {
              winner = "X";
              o_win_counter++;
          }
  
          MessageBox.Show(winner + " Wins!", "Winner");
      }
      else
      {
          if(turn_count==9)
              MessageBox.Show("Draw", "Draw!");
  
  
      }
  }
  
  private void disableButtons()
  {
      try
      {
          foreach (Control control in controlGrid.Children)
          {
              if (control is Button)
              {
                  Button button = (Button)control;
                  button.IsEnabled = false;
              }
          }
      }
      catch { }
  }
  
  private void newButton_Click(object sender, RoutedEventArgs e)
  {
      turn = true;
      turn_count = 0;
  
      try
      {
          foreach (Control control in controlGrid.Children)
          {
              if (control is Button)
              {
                  Button button = (Button)control;
                  button.IsEnabled = true;
                  button.Content = "";
              }
          }
      }
      catch { }
  }
  
  private void button_enter(object sender, MouseEventArgs e)
  {
      Button button = (Button)sender;
      if (button.IsEnabled)
      {
          if (turn)
              button.Content = "X";
          else
              button.Content = "O";
      }
  }
  
  private void button_leave(object sender, MouseEventArgs e)
  {
      Button button = (Button)sender;
      if (button.IsEnabled)
          button.Content = "";
  
  
  
     } 
    }
  }
  </code></pre>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>&lt;Window x:Class="FirstApplication.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="500" Width="525" Name="MyWindow"&gt;        
  
  &lt;Grid Margin="0,0,0.4,-19.2" Name="controlGrid"&gt;
      &lt;Button x:Name="A" Content="" HorizontalAlignment="Left" Margin="117,59,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/&gt;
      &lt;Button x:Name="B" Content="" HorizontalAlignment="Left" Margin="223,59,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/&gt;
      &lt;Button x:Name="C" Content="" HorizontalAlignment="Left" Margin="330,59,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/&gt;
      &lt;Button x:Name="D" Content="" HorizontalAlignment="Left" Margin="117,157,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/&gt;
      &lt;Button x:Name="E" Content="" HorizontalAlignment="Left" Margin="223,157,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/&gt;
      &lt;Button x:Name="F" Content="" HorizontalAlignment="Left" Margin="330,157,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/&gt;
      &lt;Button x:Name="G" Content="" HorizontalAlignment="Left" Margin="117,255,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/&gt;
      &lt;Button x:Name="H" Content="" HorizontalAlignment="Left" Margin="223,255,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/&gt;
      &lt;Button x:Name="I" Content="" HorizontalAlignment="Left" Margin="330,255,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/&gt;
      &lt;ToolBar Margin="0,0,0,450.6"&gt;
          &lt;Button Name="newButton" Click="newButton_Click"&gt;New&lt;/Button&gt;
      &lt;/ToolBar&gt;
      &lt;Label Content="X Win Count" HorizontalAlignment="Left" Margin="117,369,0,0" VerticalAlignment="Top" FontWeight="Bold"/&gt;
      &lt;Label Content="Draw Count" HorizontalAlignment="Left" Margin="233,369,0,0" VerticalAlignment="Top" FontWeight="Bold"/&gt;
      &lt;Label Content="O Win Count" HorizontalAlignment="Left" Margin="348,369,0,0" VerticalAlignment="Top" FontWeight="Bold"/&gt;
      &lt;Label x:Name="x_win_count" Content="0" HorizontalAlignment="Left" Margin="138,400,0,0" VerticalAlignment="Top"/&gt;
      &lt;Label x:Name="draw_count" Content="0" HorizontalAlignment="Left" Margin="259,400,0,0" VerticalAlignment="Top"/&gt;
      &lt;Label x:Name="o_win_count" Content="{Binding Path=_o_win_counter, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="381,400,0,0" VerticalAlignment="Top"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>