在wpf c#应用程序中的其他另一个类中访问XAML的按钮和复选框的值

我正在研究WPF Kinect项目。 它是Windows Kinect的开发人员工具包示例之一,它被称为“Kinect Explorer”。 您可以从Kinect Developer Toolkit SDK ver 1.5下载它。 在kinectwindow.xaml中,我添加了一个按钮和一个复选框。 此外,还有一个名为kinectskeleton.cs的类,其中我创建了两个DataTables和一个布尔变量。 第一个DataTable填充在OnRender函数中,而另一个是空的。 布尔变量默认设置为false。 所以,我想要的是当按下kinectwindow.xaml.cs中的按钮时,填充的DataTable的最新数据被复制到空的DataTable中。 然后,选中该复选框时,布尔值设置为true。 那么,怎么做呢?

我在类kinectskeleton.cs中定义了一个函数,它将数据从填充的数据复制到空的DataTable 。 在kinectwindow.xaml.cs按钮的OnClick函数中,我从类kinectskeleton创建了一个对象并调用了这个函数,但两个DataTable都是空的。 CheckBox_Checked函数中的CheckBox_Checked :我将类kinectskelton的布尔值设置为true(在未选中的函数中,我将其设置为false)。 但是,结果是在kinectskelton类中它始终设置为默认值(false),并且永远不会输入我为它输入的if条件。

希望现在更清楚,等待任何建议。 要下载该工具包,请访问以下链接: http : //www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx

我的代码的一部分:

 //------------------------------------------------------------------------------ //  // Copyright (c) Microsoft Corporation. All rights reserved. //  //------------------------------------------------------------------------------ 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; ///  /// Interaction logic for KinectWindow.xaml. ///  public partial class KinectWindow : Window { public static readonly DependencyProperty KinectSensorProperty = DependencyProperty.Register( "KinectSensor", typeof(KinectSensor), typeof(KinectWindow), new PropertyMetadata(null)); private readonly KinectWindowViewModel viewModel; ///  /// Initializes a new instance of the KinectWindow class, which provides access to many KinectSensor settings /// and output visualization. ///  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; } } } ///  /// A ViewModel for a KinectWindow. ///  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; ///  /// 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. ///  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(); } } } 

复选框的xaml代码:

                                  Kinect Explorer                   FPS                           FPS          

ViewModel类:

 public class ViewModel { public bool IsChecked { get; set; } public bool is_clicked { get; set; } } 

如果没有看到完整的KinectWindow.xaml文件或下载SDK,我会做一些猜测,但在我看来,你的问题是由于有两个不同的KinectSkeleton实例KinectSkeleton

  • 您正在KinectWindow实例化的KinectWindow ,设置其is_checked属性并调用is_checked
  • KinectWindow.xaml中某处KinectWindow.xaml ,其中正在调用OnRender

如何解决这个问题?

  • KinectWindow.xaml找到KinectWindow.xaml 。 它应该像这样定义(而不是local ,可以使用不同的名称空间前缀):
  
  • 为它命名,以便您可以通过添加x:Name属性从KinextWindow.xaml.cs的代码引用它。 如果您将其命名为ks ,则无需更改现有的代码修改:
  
  • 在代码后面删除你的KinectSkeleton类声明以防止冲突:
 public KinectSkeleton ks = new KinectSkeleton(); 

现在您将只有一个KinectSkeleton实例,因此OnRender将使用您从事件处理程序修改的相同数据。