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が言ったように、ウィンドウが読み込まれると、変数の値xInfoが変更されたことをWPF TextBoxに通知する必要があるため、モデルクラスで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 your 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 に答える