title: Access the value of button and checkbox of an XAML in other another class in a wpf c# application
body: <p>I'm working on a WPF Kinect project. It's one of the developer toolkit samples of Windows Kinect and it's called "Kinect Explorer". You can download it from the Kinect Developer Toolkit SDK ver 1.5. In kinectwindow.xaml I added a button and a checkbox. Also, there is a class called kinectskeleton.cs in which I created two DataTables and a boolean variables. The first <code>DataTable</code> is filled in the <code>OnRender</code> function while the other is empty. The boolean variable is set by default to false. So, what I want is when the button in the kinectwindow.xaml.cs is pressed the latest data in the filled <code>DataTable</code> is copied to the empty one. Then, when the checkbox is checked, the boolean value is set to true. So, how to do this?</p>
  
  <p>I defined a function in the class kinectskeleton.cs that copies the data from the filled to the empty <code>DataTable</code>. In the <code>OnClick</code> function of the button of kinectwindow.xaml.cs, I created an object from class <code>kinectskeleton</code> and called this function but both DataTables are empty. Same for the checkbox in the <code>CheckBox_Checked</code> function: I set the boolean value of the class <code>kinectskelton</code> to true (and in the unchecked function I set it to false). But, the result is that in the <code>kinectskelton</code> class it's always set to the default value (false) and never enters the if condition I made for it to enter when it's true.</p>
  
  <p>Hope it's now more clear and waiting for any suggestions. To download the toolkit, here is the link: <a href="http://www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx" rel="nofollow">http://www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx</a></p>
  
  <p>part of my code:</p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;copyright file="KinectWindow.xaml.cs" company="Microsoft"&gt;
  //     Copyright (c) Microsoft Corporation.  All rights reserved.
  // &lt;/copyright&gt;
  //------------------------------------------------------------------------------
  
  namespace Microsoft.Samples.Kinect.KinectExplorer
  {
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Data;
      using System.Windows.Input;
      using Microsoft.Kinect;
      using Microsoft.Samples.Kinect.WpfViewers;
  
      /// &lt;summary&gt;
      /// Interaction logic for KinectWindow.xaml.
      /// &lt;/summary&gt;
      public partial class KinectWindow : Window
      {
          public static readonly DependencyProperty KinectSensorProperty =
              DependencyProperty.Register(
                  "KinectSensor",
                  typeof(KinectSensor),
                  typeof(KinectWindow),
                  new PropertyMetadata(null));
  
          private readonly KinectWindowViewModel viewModel;
          /// &lt;summary&gt;
          /// Initializes a new instance of the KinectWindow class, which provides access to many KinectSensor settings
          /// and output visualization.
          /// &lt;/summary&gt;
          public KinectWindow()
          {
              this.viewModel = new KinectWindowViewModel();
  
              // The KinectSensorManager class is a wrapper for a KinectSensor that adds
              // state logic and property change/binding/etc support, and is the data model
              // for KinectDiagnosticViewer.
              this.viewModel.KinectSensorManager = new KinectSensorManager();
  
              Binding sensorBinding = new Binding("KinectSensor");
              sensorBinding.Source = this;
              BindingOperations.SetBinding(this.viewModel.KinectSensorManager, KinectSensorManager.KinectSensorProperty, sensorBinding);
  
              // Attempt to turn on Skeleton Tracking for each Kinect Sensor
              this.viewModel.KinectSensorManager.SkeletonStreamEnabled = true;
  
              this.DataContext = this.viewModel;
  
              InitializeComponent();
          }
  
          public KinectSensor KinectSensor
          {
              get { return (KinectSensor)GetValue(KinectSensorProperty); }
              set { SetValue(KinectSensorProperty, value); }
          }
  
          public void StatusChanged(KinectStatus status)
          {
              this.viewModel.KinectSensorManager.KinectSensorStatus = status;
          }
  
          private void Swap_Executed(object sender, ExecutedRoutedEventArgs e)
          {
              Grid colorFrom = null;
              Grid depthFrom = null;
  
              if (this.MainViewerHost.Children.Contains(this.ColorVis))
              {
                  colorFrom = this.MainViewerHost;
                  depthFrom = this.SideViewerHost;
              }
              else
              {
                  colorFrom = this.SideViewerHost;
                  depthFrom = this.MainViewerHost;
              }
  
              colorFrom.Children.Remove(this.ColorVis);
              depthFrom.Children.Remove(this.DepthVis);
              colorFrom.Children.Insert(0, this.DepthVis);
              depthFrom.Children.Insert(0, this.ColorVis);
          }
          public KinectSkeleton ks = new KinectSkeleton();
          private void Calibrate_Click(object sender, RoutedEventArgs e)
          {
  
              ks.setcurrentdt();
  
          }
  
          private void AngleDifference_Checked(object sender, RoutedEventArgs e)
          {
  
              ks.is_checked = true;
          }
  
          private void AngleDifference_Unchecked(object sender, RoutedEventArgs e)
          {
              ks.is_checked = false;
          }
  
  
              }
  }
  
      /// &lt;summary&gt;
      /// A ViewModel for a KinectWindow.
      /// &lt;/summary&gt;
      public class KinectWindowViewModel : DependencyObject
      {
          public static readonly DependencyProperty KinectSensorManagerProperty =
              DependencyProperty.Register(
                  "KinectSensorManager",
                  typeof(KinectSensorManager),
                  typeof(KinectWindowViewModel),
                  new PropertyMetadata(null));
  
          public KinectSensorManager KinectSensorManager
          {
              get { return (KinectSensorManager)GetValue(KinectSensorManagerProperty); }
              set { SetValue(KinectSensorManagerProperty, value); }
          }
      }
  
  
  }
  
  namespace Microsoft.Samples.Kinect.WpfViewers
  {
      using System.Collections.Generic;
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Media;
      using Microsoft.Kinect;
      using System.Data;
      using System.Windows.Media.Media3D;
      using System.Globalization;
  
      /// &lt;summary&gt;
      /// This control is used to render a player's skeleton.
      /// If the ClipToBounds is set to "false", it will be allowed to overdraw
      /// it's bounds.
      /// &lt;/summary&gt;
      public class KinectSkeleton : Control
      {
          public bool is_checked=false;
          public DataTable x = new DataTable();
          public DataTable y = new DataTable();
  protected override void OnRender(DrawingContext drawingContext)
          {
              base.OnRender(drawingContext);
  
              var currentSkeleton = this.Skeleton;
  
              // Don't render if we don't have a skeleton, or it isn't tracked
              if (drawingContext == null || currentSkeleton == null || currentSkeleton.TrackingState == SkeletonTrackingState.NotTracked)
              {
                  return;
              }
  
              // Displays a gradient near the edge of the display where the skeleton is leaving the screen
              this.RenderClippedEdges(drawingContext);
  
              switch (currentSkeleton.TrackingState)
              {
                  case SkeletonTrackingState.PositionOnly:
                      if (this.ShowCenter)
                      {
                          drawingContext.DrawEllipse(
                              this.centerPointBrush,
                              null,
                              this.Center,
                              BodyCenterThickness * this.ScaleFactor,
                              BodyCenterThickness * this.ScaleFactor);
                      }
  
                      break;
                  case SkeletonTrackingState.Tracked:
  
  
                     // here i defined the DataTables
  
  
  
                          if (is_checked == false)
                          {
                              //fill data table x with value a
                          }
                          else
                          {
                              //fill data table x with value b
                          }
  
                      break;
              }
          }
  
   public void setcurrentdt()
          {
  
              //fill empty datatable "y" with filled one "x"
                y = x.Copy();
          }
      }
  }
  </code></pre>
  
  <p>xaml code of the checkbox:</p>
  
  <pre><code>   &lt;Window x:Class="Microsoft.Samples.Kinect.KinectExplorer.KinectWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:Microsoft.Samples.Kinect.KinectExplorer"
          xmlns:kt="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers"
          Title="Kinect Explorer" Width="839" Height="768"&gt;
      &lt;Window.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source="pack://application:,,,/Microsoft.Samples.Kinect.WpfViewers;component/KinectControlResources.xaml"/&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
              &lt;local:KinectWindowsViewerSwapCommand x:Key="SwapCommand"/&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/Window.Resources&gt;
      &lt;Window.CommandBindings&gt;
          &lt;CommandBinding Command="{StaticResource SwapCommand}" Executed="Swap_Executed"/&gt;
      &lt;/Window.CommandBindings&gt;
      &lt;Window.InputBindings&gt;
          &lt;KeyBinding Key="Back"  Command="{StaticResource SwapCommand}"/&gt;
      &lt;/Window.InputBindings&gt;
  
      &lt;Grid Name="layoutGrid" Margin="10 0 10 0" TextBlock.FontFamily="{StaticResource KinectFont}"&gt;        
          &lt;Grid.RowDefinitions&gt;
              &lt;!-- The title bar --&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;!-- The main viewer --&gt;
              &lt;RowDefinition Height="*" MinHeight="300"/&gt;
              &lt;!-- The audio panel --&gt;
              &lt;RowDefinition Height="Auto"/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;!-- The main viewer --&gt;
              &lt;ColumnDefinition Width="*" MinWidth="400"/&gt;
              &lt;!-- The side panels --&gt;
              &lt;ColumnDefinition Width="Auto"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;DockPanel Grid.Row="0" Grid.ColumnSpan="2" Margin="10 0 10 20"&gt;
              &lt;Image DockPanel.Dock="Left" Source="Images\Logo.png" Stretch="None" HorizontalAlignment="Left" Margin="0 10 0 0"/&gt;
              &lt;TextBlock DockPanel.Dock="Right" 
                         HorizontalAlignment="Right" 
                         VerticalAlignment="Bottom" 
                         Foreground="{StaticResource TitleForegroundBrush}" FontSize="{StaticResource LabelFontSize}"&gt;Kinect Explorer&lt;/TextBlock&gt;
              &lt;Image Source="Images\Status.png" Stretch="None" HorizontalAlignment="Center"/&gt;
          &lt;/DockPanel&gt;
  
          &lt;!-- The main viewer --&gt;
          &lt;Grid Grid.Column="0" Grid.Row="1" Margin="10" &gt;
              &lt;Grid Name="MainViewerHost"&gt;
                  &lt;Grid Name="ColorVis" Background="{StaticResource DarkNeutralBrush}"&gt;
                      &lt;Viewbox HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform"&gt;
                          &lt;!-- Make the colorViewer and skeletonViewer overlap entirely. --&gt;
                          &lt;Grid&gt;
                              &lt;kt:KinectColorViewer x:Name="ColorViewer" KinectSensorManager="{Binding KinectSensorManager}" CollectFrameRate="True" RetainImageOnSensorChange="True" /&gt;
                              &lt;Canvas&gt;
                                  &lt;kt:KinectSkeletonViewer 
                                      KinectSensorManager="{Binding KinectSensorManager}"
                                      Visibility="{Binding KinectSensorManager.ColorStreamEnabled, Converter={StaticResource BoolToVisibilityConverter}}"
                                      Width="{Binding ElementName=ColorViewer,Path=ActualWidth}"
                                      Height="{Binding ElementName=ColorViewer,Path=ActualHeight}"
                                      ImageType="Color" /&gt;
                              &lt;/Canvas&gt;
                          &lt;/Grid&gt;
                      &lt;/Viewbox&gt;
                      &lt;Border 
                          TextBlock.Foreground="{StaticResource LabelForegroundBrush}" 
                          HorizontalAlignment="Right" VerticalAlignment="Top" 
                          Background="{StaticResource MediumNeutralBrush}"
                          Width="50" Height="50"&gt;
                          &lt;StackPanel Orientation="Vertical" &gt;
                              &lt;TextBlock FontSize="{StaticResource HeaderFontSize}" Text="{Binding ElementName=ColorViewer, Path=FrameRate}" HorizontalAlignment="Center" Margin="-2"/&gt;
                              &lt;TextBlock FontSize="{StaticResource FPSFontSize}" HorizontalAlignment="Center" Margin="-2"&gt;FPS&lt;/TextBlock&gt;
                          &lt;/StackPanel&gt;
                      &lt;/Border&gt;                   
                      &lt;Rectangle Fill="#7777" Visibility="{Binding KinectSensorManager.ColorStreamEnabled, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=True}"/&gt;
                  &lt;/Grid&gt;
              &lt;/Grid&gt;
          &lt;/Grid&gt;
  
          &lt;!-- The Audio panel --&gt;
          &lt;Grid Grid.Row="2" Grid.Column="0"
                Margin="10 10 10 20"
                VerticalAlignment="Top"&gt;
              &lt;kt:KinectAudioViewer 
                  x:Name="kinectAudioViewer" 
                  HorizontalAlignment="Stretch" 
                  VerticalAlignment="Stretch"                 
                  KinectSensorManager="{Binding KinectSensorManager}"/&gt;
          &lt;/Grid&gt;
  
          &lt;!-- The side panels--&gt;
          &lt;StackPanel 
              Orientation="Vertical" 
              Grid.Column="1" 
              Grid.Row="1" 
              Grid.RowSpan="2" 
              Margin="10"
              HorizontalAlignment="Left" VerticalAlignment="Top"&gt;
              &lt;Grid Name="SideViewerHost" Width="200" Height="150"&gt;
                  &lt;Grid Name="DepthVis" Background="{StaticResource DarkNeutralBrush}"&gt;
                      &lt;Viewbox Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center"&gt;
                          &lt;!-- Make the depthViewer and skeletonViewer overlap entirely. --&gt;
                          &lt;Grid&gt;
                              &lt;kt:KinectDepthViewer 
                              x:Name="DepthViewer"
                              KinectSensorManager="{Binding KinectSensorManager}"
                              CollectFrameRate="True" 
                              RetainImageOnSensorChange="True"/&gt;
                              &lt;Canvas&gt;
                                  &lt;kt:KinectSkeletonViewer 
                                  KinectSensorManager="{Binding KinectSensorManager}"
                                  Visibility="{Binding KinectSensorManager.DepthStreamEnabled, Converter={StaticResource BoolToVisibilityConverter}}"
                                  Width="{Binding ElementName=DepthViewer, Path=ActualWidth}"
                                  Height="{Binding ElementName=DepthViewer, Path=ActualHeight}"
                                  ShowBones="true" ShowJoints="true" ShowCenter="true" ImageType="Depth" /&gt;
                              &lt;/Canvas&gt;
                          &lt;/Grid&gt;
                      &lt;/Viewbox&gt;
                      &lt;Border 
                          TextBlock.Foreground="{StaticResource LabelForegroundBrush}" 
                          HorizontalAlignment="Right" VerticalAlignment="Top" 
                          Background="{StaticResource MediumNeutralBrush}"
                          Width="50" Height="50"&gt;
                          &lt;StackPanel Orientation="Vertical" &gt;                            
                              &lt;TextBlock FontSize="{StaticResource HeaderFontSize}" Text="{Binding ElementName=DepthViewer, Path=FrameRate}" HorizontalAlignment="Center" Margin="-2"/&gt;
                              &lt;TextBlock FontSize="{StaticResource FPSFontSize}" HorizontalAlignment="Center" Margin="-2"&gt;FPS&lt;/TextBlock&gt;
                          &lt;/StackPanel&gt;
                      &lt;/Border&gt;
                      &lt;Rectangle Fill="#7777" Visibility="{Binding KinectSensorManager.DepthStreamEnabled, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=True}"/&gt;
                  &lt;/Grid&gt;
                  &lt;Button HorizontalAlignment="Left" VerticalAlignment="Top" Command="{StaticResource SwapCommand}"&gt;
                      &lt;Button.Template&gt;
                          &lt;ControlTemplate&gt;
                              &lt;Border Width="50" Height="50"&gt;
                                  &lt;Border.Style&gt;
                                      &lt;Style&gt;
                                          &lt;Style.Setters&gt;
                                              &lt;Setter Property="Border.Background" Value="{StaticResource MediumNeutralBrush}"/&gt;
                                          &lt;/Style.Setters&gt;
                                          &lt;Style.Triggers&gt;
                                              &lt;DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.TemplatedParent}, Path=IsMouseOver}" Value="True"&gt;
                                                  &lt;Setter Property="Border.Background" Value="{StaticResource DarkNeutralBrush}"/&gt;
                                              &lt;/DataTrigger&gt;
                                          &lt;/Style.Triggers&gt;
                                      &lt;/Style&gt;
                                  &lt;/Border.Style&gt;
                                  &lt;Image Source="Images/swap.png"/&gt;
                              &lt;/Border&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/Button.Template&gt;
                  &lt;/Button&gt;                
              &lt;/Grid&gt;
  
              &lt;kt:KinectSettings KinectSensorManager="{Binding KinectSensorManager}" Margin="0 20 0 0" /&gt;
  
          &lt;/StackPanel&gt;
          &lt;Button Content="Calibrate"  Height="23" x:Name="Calibrate" x:FieldModifier="public" Width="75" Margin="213,45,289,435" Grid.RowSpan="2" Click="Calibrate_Click" /&gt;
          &lt;CheckBox Content="AngleDifference"  Height="25" x:Name="AngleDifference" x:FieldModifier="public" Width="135" Margin="0,45,137,433" Grid.RowSpan="2" Checked="AngleDifference_Checked" HorizontalAlignment="Right" Unchecked="AngleDifference_Unchecked" /&gt;
      &lt;/Grid&gt; 
  &lt;/Window&gt;
  </code></pre>
  
  <p>ViewModel class:</p>
  
  <pre><code>public class ViewModel
      {
  
          public bool IsChecked { get; set; }
          public bool is_clicked { get; set; }
  
      }
  </code></pre>