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

2023-12-03

我正在开发 WPF Kinect 项目。它是 Windows Kinect 的开发人员工具包示例之一,称为“Kinect Explorer”。您可以从 Kinect 开发者工具包 SDK 1.5 版下载它。在 kinectwindow.xaml 中,我添加了一个按钮和一个复选框。另外,还有一个名为 kinectsculpture.cs 的类,我在其中创建了两个数据表和一个布尔变量。首先DataTable被填入OnRender功能,而另一个是空的。布尔变量默认设置为 false。所以,我想要的是当按下 kinectwindow.xaml.cs 中的按钮时填充的最新数据DataTable被复制到空的位置。然后,当选中该复选框时,布尔值将设置为 true。那么,如何做到这一点呢?

我在 kinectsculpture.cs 类中定义了一个函数,它将数据从填充的复制到空的DataTable。在里面OnClickkinectwindow.xaml.cs按钮的功能,我从类创建了一个对象kinectskeleton并调用了这个函数,但两个数据表都是空的。与中的复选框相同CheckBox_Checked功能:我设置类的布尔值kinectskelton为 true (并且在未经检查的函数中我将其设置为 false)。但是,结果是,在kinectskeltonclass 它总是设置为默认值 (false),并且永远不会进入我为它设置的 if 条件,当它为 true 时进入。

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

我的代码的一部分:

//------------------------------------------------------------------------------
// <copyright file="KinectWindow.xaml.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

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;

    /// <summary>
    /// Interaction logic for KinectWindow.xaml.
    /// </summary>
    public partial class KinectWindow : Window
    {
        public static readonly DependencyProperty KinectSensorProperty =
            DependencyProperty.Register(
                "KinectSensor",
                typeof(KinectSensor),
                typeof(KinectWindow),
                new PropertyMetadata(null));

        private readonly KinectWindowViewModel viewModel;
        /// <summary>
        /// Initializes a new instance of the KinectWindow class, which provides access to many KinectSensor settings
        /// and output visualization.
        /// </summary>
        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;
        }


            }
}

    /// <summary>
    /// A ViewModel for a KinectWindow.
    /// </summary>
    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;

    /// <summary>
    /// 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.
    /// </summary>
    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代码:

   <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">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Microsoft.Samples.Kinect.WpfViewers;component/KinectControlResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <local:KinectWindowsViewerSwapCommand x:Key="SwapCommand"/>
        </ResourceDictionary>
    </Window.Resources>
    <Window.CommandBindings>
        <CommandBinding Command="{StaticResource SwapCommand}" Executed="Swap_Executed"/>
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Key="Back"  Command="{StaticResource SwapCommand}"/>
    </Window.InputBindings>

    <Grid Name="layoutGrid" Margin="10 0 10 0" TextBlock.FontFamily="{StaticResource KinectFont}">        
        <Grid.RowDefinitions>
            <!-- The title bar -->
            <RowDefinition Height="Auto"/>
            <!-- The main viewer -->
            <RowDefinition Height="*" MinHeight="300"/>
            <!-- The audio panel -->
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <!-- The main viewer -->
            <ColumnDefinition Width="*" MinWidth="400"/>
            <!-- The side panels -->
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <DockPanel Grid.Row="0" Grid.ColumnSpan="2" Margin="10 0 10 20">
            <Image DockPanel.Dock="Left" Source="Images\Logo.png" Stretch="None" HorizontalAlignment="Left" Margin="0 10 0 0"/>
            <TextBlock DockPanel.Dock="Right" 
                       HorizontalAlignment="Right" 
                       VerticalAlignment="Bottom" 
                       Foreground="{StaticResource TitleForegroundBrush}" FontSize="{StaticResource LabelFontSize}">Kinect Explorer</TextBlock>
            <Image Source="Images\Status.png" Stretch="None" HorizontalAlignment="Center"/>
        </DockPanel>

        <!-- The main viewer -->
        <Grid Grid.Column="0" Grid.Row="1" Margin="10" >
            <Grid Name="MainViewerHost">
                <Grid Name="ColorVis" Background="{StaticResource DarkNeutralBrush}">
                    <Viewbox HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform">
                        <!-- Make the colorViewer and skeletonViewer overlap entirely. -->
                        <Grid>
                            <kt:KinectColorViewer x:Name="ColorViewer" KinectSensorManager="{Binding KinectSensorManager}" CollectFrameRate="True" RetainImageOnSensorChange="True" />
                            <Canvas>
                                <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" />
                            </Canvas>
                        </Grid>
                    </Viewbox>
                    <Border 
                        TextBlock.Foreground="{StaticResource LabelForegroundBrush}" 
                        HorizontalAlignment="Right" VerticalAlignment="Top" 
                        Background="{StaticResource MediumNeutralBrush}"
                        Width="50" Height="50">
                        <StackPanel Orientation="Vertical" >
                            <TextBlock FontSize="{StaticResource HeaderFontSize}" Text="{Binding ElementName=ColorViewer, Path=FrameRate}" HorizontalAlignment="Center" Margin="-2"/>
                            <TextBlock FontSize="{StaticResource FPSFontSize}" HorizontalAlignment="Center" Margin="-2">FPS</TextBlock>
                        </StackPanel>
                    </Border>                   
                    <Rectangle Fill="#7777" Visibility="{Binding KinectSensorManager.ColorStreamEnabled, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=True}"/>
                </Grid>
            </Grid>
        </Grid>

        <!-- The Audio panel -->
        <Grid Grid.Row="2" Grid.Column="0"
              Margin="10 10 10 20"
              VerticalAlignment="Top">
            <kt:KinectAudioViewer 
                x:Name="kinectAudioViewer" 
                HorizontalAlignment="Stretch" 
                VerticalAlignment="Stretch"                 
                KinectSensorManager="{Binding KinectSensorManager}"/>
        </Grid>

        <!-- The side panels-->
        <StackPanel 
            Orientation="Vertical" 
            Grid.Column="1" 
            Grid.Row="1" 
            Grid.RowSpan="2" 
            Margin="10"
            HorizontalAlignment="Left" VerticalAlignment="Top">
            <Grid Name="SideViewerHost" Width="200" Height="150">
                <Grid Name="DepthVis" Background="{StaticResource DarkNeutralBrush}">
                    <Viewbox Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center">
                        <!-- Make the depthViewer and skeletonViewer overlap entirely. -->
                        <Grid>
                            <kt:KinectDepthViewer 
                            x:Name="DepthViewer"
                            KinectSensorManager="{Binding KinectSensorManager}"
                            CollectFrameRate="True" 
                            RetainImageOnSensorChange="True"/>
                            <Canvas>
                                <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" />
                            </Canvas>
                        </Grid>
                    </Viewbox>
                    <Border 
                        TextBlock.Foreground="{StaticResource LabelForegroundBrush}" 
                        HorizontalAlignment="Right" VerticalAlignment="Top" 
                        Background="{StaticResource MediumNeutralBrush}"
                        Width="50" Height="50">
                        <StackPanel Orientation="Vertical" >                            
                            <TextBlock FontSize="{StaticResource HeaderFontSize}" Text="{Binding ElementName=DepthViewer, Path=FrameRate}" HorizontalAlignment="Center" Margin="-2"/>
                            <TextBlock FontSize="{StaticResource FPSFontSize}" HorizontalAlignment="Center" Margin="-2">FPS</TextBlock>
                        </StackPanel>
                    </Border>
                    <Rectangle Fill="#7777" Visibility="{Binding KinectSensorManager.DepthStreamEnabled, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=True}"/>
                </Grid>
                <Button HorizontalAlignment="Left" VerticalAlignment="Top" Command="{StaticResource SwapCommand}">
                    <Button.Template>
                        <ControlTemplate>
                            <Border Width="50" Height="50">
                                <Border.Style>
                                    <Style>
                                        <Style.Setters>
                                            <Setter Property="Border.Background" Value="{StaticResource MediumNeutralBrush}"/>
                                        </Style.Setters>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.TemplatedParent}, Path=IsMouseOver}" Value="True">
                                                <Setter Property="Border.Background" Value="{StaticResource DarkNeutralBrush}"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Border.Style>
                                <Image Source="Images/swap.png"/>
                            </Border>
                        </ControlTemplate>
                    </Button.Template>
                </Button>                
            </Grid>

            <kt:KinectSettings KinectSensorManager="{Binding KinectSensorManager}" Margin="0 20 0 0" />

        </StackPanel>
        <Button Content="Calibrate"  Height="23" x:Name="Calibrate" x:FieldModifier="public" Width="75" Margin="213,45,289,435" Grid.RowSpan="2" Click="Calibrate_Click" />
        <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" />
    </Grid> 
</Window>

视图模型类:

public class ViewModel
    {

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

    }

没有看到完整的KinectWindow.xaml文件或下载 SDK 我有点猜测,但在我看来,您的问题是由两个不同的实例引起的KinectSkeleton:

  • ks你正在实例化的KinectWindow,设置其is_checked财产和召唤setcurrentdt
  • 那个在某处声明的KinectWindow.xaml其中OnRender正在被呼叫。

如何解决这个问题?

  • Find KinectSkeleton in KinectWindow.xaml。它应该这样定义(而不是local,可以使用不同的命名空间前缀):
<local:KinectSkeleton ... />
  • 给它一个名称,以便您可以从后面的代码中引用它KinextWindow.xaml.cs通过添加x:Name属性。如果你命名它ks您不需要更改现有的代码修改:
<local:KinectSkeleton x:Name="ks" ... />
  • 删除您的声明KinectSkeleton代码后面的类以防止冲突:
public KinectSkeleton ks = new KinectSkeleton();

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

访问 wpf c# 应用程序中其他类中 XAML 的按钮和复选框的值 的相关文章

随机推荐

  • 如何使用 livedata 从服务更新 UI?

    我正在创建播放器应用程序 我想为我的媒体播放器使用前台服务 我需要使用服务中的实时数据更新 UI 我已经阅读过有关使用广播接收器的信息 但我想使用 livedata 假设您使用具有实时数据的服务 如下所示 class MusicServic
  • 组合时间序列对象和列表:包“termstrc”

    R 包 termstrc 专为术语结构估计而设计 是一个非常有用的工具 但它需要以一种特别尴尬的格式设置数据 列表中的列表 Question 为了创建运行函数 dyncouponbonds 所需的重复子列表格式 在 R 外部或内部准备和调整
  • 更新 pygame 中的文本

    我在这里做错了什么 我想更新标签的文本以适应玩家得分 我查看了其他示例并添加了更新方法 但文本仍然保持不变 class Label def init self txt location size 160 30 bg WHITE fg BLA
  • youtube 嵌入视频 pregreplace 与开始计时

    在我们的论坛上 我们目前用嵌入对象替换了所有 YouTube 链接 感谢以下答案 如何使用正则表达式查找字符串中的所有 YouTube 视频 ID 问题是 我们的许多用户希望直接发布指向视频中特定时间的链接 例如 注意 t 1m15s 根据
  • SQL Bigquery:将特定组的选择限制为 10

    下面是示例表 目前 该表对于每个 ID 都有无限的条目 我的要求是 首先按 rand 升序对 ID 进行排序 然后只取前 2 行 ID 和 companies CREATE TABLE table name ID int companies
  • 将 Jetty 绑定到 IPv6 地址

    我正在尝试将 Jetty 绑定为仅侦听 IPv6 地址 我正在使用 Jetty 7 4 2 v20110526 我的jetty xml
  • 在图像上绘制对角线

    您好 我正在尝试在图像的右上角到左下角之间绘制对角线 这是到目前为止我的代码 width getWidth picture height getHeight picture for x in range 0 width for y in r
  • 根据月份日期向数据表添加季节列

    我正在使用 data table 我正在尝试创建一个名为 season 的新列 它基于名为 MonthName 的列创建一个具有相应季节的列 例如夏季 冬季 我想知道是否有更有效的方法来根据月份值将季节列添加到数据表中 这是 300 000
  • 默认情况下是否启用可选依赖项?

    如果我定义一个依赖项 例如foo version 1 0 0 optional true 当我执行 cargo run 时它会可用吗 我可以检查它是否在代码中启用吗 if cfg feature foo 似乎不起作用 就像该功能一直缺失一样
  • matplotlib 条形图中的极限误差线

    我试图让误差条显示在置信区间的限制处 而不是显示在中心 我想要的是这样的 但我得到的是这样的 为了绘制条形图 我使用了这个 import pandas as pd import numpy as np import matplotlib p
  • 验证视图状态 MAC 失败错误

    尝试通过传递参数来运行报表查看器 但收到错误 验证视图状态 MAC 失败错误 ASP NET MVC 已尝试以下但没有运气 添加了机器密钥 http aspnetresources com tools machineKey 到 web co
  • Hyperledger Fabric 加密材料

    如果我们看到加密配置文件夹中基础网络 of 布料样品 我们有各种类型的各种证书材料 example com ca 0d46ccf0e9436c1bc3b6e2bf80cdb202c4943604f95c72ee0ff839d3ec30071
  • 由于名称中存在撇号而导致无效的 XPath 表达式异常

    我收到以下代码的无效 Xpath 异常 current Name current Name replace System out println current Name String xp1 page name current Name
  • 在 web.config 文件中设置重定向

    我正在尝试使用更具描述性的 URL 来重定向一些不友好的 URL 这些 URL 结尾为 aspx cid 3916每个类别名称页面的最后一位数字都不同 我希望它重定向到Category CategoryName 3916 我在web con
  • Android:如何创建“持续”通知?

    您好 我如何创建像第一个电池指示器一样的永久通知 如果您正在使用NotificationCompat Builder 您可以使用 NotificationCompat Builder mBuilder new NotificationCom
  • 从本地 html/javascript 网站发布到在线 PHP 文件

    我正在尝试做什么 从本地 html javascript 网站发布到在线 PHP 文件 Problem 当我尝试使用下面的代码时 我不断收到下面提到的错误 背景 该网站旨在本地运行 由于每个用户都可以选择使用哪个浏览器 因此我希望找到一种可
  • 将自定义计算添加到 magento 中的购物车总计和总计

    我正在网站上工作 我想在购物车总额和总计中添加 减去费用 我正在触发此事件以捕获购物车详细信息 sales order save after 在观察者中我使用此代码获得了价格 public function modifyPrice Vari
  • 使用 awk 或 sed 基于公共列合并两个 csv 文件 [重复]

    这个问题在这里已经有答案了 我有一个两个 CSV 文件 两个文件中有一个公共列 并且一个文件中有重复项 如何使用 awk 或 sed 合并两个 csv 文件 CSV 文件 1 5 1 20 user mark Type1 445566 5
  • 如何为for循环中除最后一项之外的每一项添加分隔符

    在下面的循环中 如何从循环中的latt键中删除逗号 var result These are the results jQuery each item keyterms terms function i kw for key in keyw
  • 访问 wpf c# 应用程序中其他类中 XAML 的按钮和复选框的值

    我正在开发 WPF Kinect 项目 它是 Windows Kinect 的开发人员工具包示例之一 称为 Kinect Explorer 您可以从 Kinect 开发者工具包 SDK 1 5 版下载它 在 kinectwindow xam