2

我对 WPF 很陌生。我目前正在编写代码以使用 Kinect SDK 检测关节坐标并显示在 WPF 中的简单文本框上。检测关节的代码位于私有 void Window_Loaded(object sender, RoutedEventArgs e) 方法中。为了显示坐标,我使用了 DataContext。事不宜迟,让我们看看 XAML 代码:

<Window x:Class="Prototype.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="480" Width="640">
<Grid>
    <TextBox x:Name="coordinateText" Width="150" Height="20" Margin="441,409,27,12" Text="{Binding Path=xInfo}"/>

</Grid>

这是我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Microsoft.Research.Kinect.Nui;
using Coding4Fun.Kinect.Wpf;

namespace Prototype
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //this.DataContext = new Coordinate { xInfo = "5" };
        }

        Runtime nui = new Runtime();

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = new Coordinate { xInfo = "5" };
            nui.Initialize(RuntimeOptions.UseSkeletalTracking); //code for detecting joints

            //some code for detecting joints

        }

        public class Coordinate
        {
             public string xInfo { get; set; }
             public string yInfo { get; set; }
             public string zInfo { get; set; }
        }
    }
 }

问题是如果 this.DataContext = new Coordinate { xInfo = "5" }; 则不会在文本框中加载信息 没有放在 MainWindow 中。我必须把它放在 Window_Loaded 方法中。有什么解决办法吗?

4

1 回答 1

1

正如 Coder323 所说,当加载窗口时,您需要告诉 WPF TextBox 变量的值xInfo已更改,因此您应该在模型类中使用 INotifyPropertyChanged

那么无论你在哪里更改对象的值,它都会获取更改后的值......也只需DataContext=myCordinate在窗口构造函数中设置,然后将我的坐标设置为窗口类中的变量。

    public class Coordinate : INotifyPropertyChanged
    {
         private string xInfo;
         public string XInfo {
                                get{retun value}; 
                                set{
                                     xInfo=Value;
                                     FirePropertyChanged("XInfo")
                                   } 
                             }


         public event PropertyChangedEventHandler PropertyChanged;
         protected void FirePropertyChanged(string propertyName)
         {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
         }

   }

对其他属性执行此操作,现在您可以设置 myCordinate.XInfo="whatever you like" 的值,它会通知您的视图相应的属性已更改。

我把我的完整解决方案放在这里

我的坐标类

public class Coordinates : INotifyPropertyChanged
{
    private string xInfo;
    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    public string XInfo
    {
        get { return xInfo; }
        set
        {
            xInfo = value;
            InvokePropertyChanged(new PropertyChangedEventArgs("XInfo"));
        }
    }

    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
    }

    #endregion
}

我的 Xaml

<Window x:Class="TestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
    <TextBox Text="{Binding Path=XInfo}" Height="30" Widht="100"></TextBox>
</Grid>

我的 Xaml.cs

namespace TestApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private Coordinates myCoordinates;
    public MainWindow()
    {
        myCoordinates=new Coordinates();
        this.DataContext = myCoordinates;
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        myCoordinates.XInfo = "Acbd";
    }
}

}

是的,我制作的这个测试项目......正在运行

这可能会有所帮助:)

于 2011-11-19T06:55:10.210 回答