2
votes

I'm very new to WPF. I'm currently doing a code to detect joints coordinate using the Kinect SDK and displaying on a simple textbox in WPF. The code to detect joints are in a private void Window_Loaded(object sender, RoutedEventArgs e) method. To display the coordinates, I used DataContext. Without further ado, let's just see the XAML code:

<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>

And this is my C# code:

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; }
        }
    }
 }

The thing is the information will not be loaded in the textbox if this.DataContext = new Coordinate { xInfo = "5" }; is not placed in the MainWindow. I have to put it in the Window_Loaded method. Any solutions?

1
why cant you have the Coordinate class as a property of this main window class and just change the xInfo in window_loaded event?Coder323
thx Coder323 for the reply. But would it make a difference? If yes, why? can u also give a small example? I'm very sorry I'm still new to WPF :(user1055010
wait, I would recommend you to use MVVM where you need to implement INotifyPropertyChanged interface msdn.microsoft.com/en-us/library/ms743695.aspxCoder323
so you mean i have to set the DataContext in the OnPropertyChanged method?user1055010

1 Answers

1
votes

As Coder323 said When window is loaded you need tell WPF TextBox that the Value of the variable xInfo is changed so you should use INotifyPropertyChanged in your Model Class

then where ever you change theValue of your Object it will pick up the changed Value... also Just Set the DataContext=myCordinate in the Window Constructor then, make my cordinate a variable in the window class.

    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));
            }
         }

   }

Do this for other properties and now you can set the value of myCordinate.XInfo="what ever you like" in any event it will notify to your view that the respective property has changed..

I am putting my complete solution here

My Coordinate class

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
}

My 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>

My 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";
    }
}

}

And yes this test Project i made... is working

This might Help :)