none
WFP listbox and display of their associated values RRS feed

  • Question

  • Hello Folks

    I am writing a program that retrieves coordinates x, y, z of joints "skeletal tracking" from the Kinect sensor. Since i am kinda new in C#, i'm stopped at my progress because i wanted to display these x,y,z for each joint....there are 20 joints, of course i'm not gonna ndo them all in the window, So i wanted to create a Listbox "in WPF" that has all thejoint, each one i select i get its corresponding real time values of the coordinates. 

    I am thinking to created a multi-dimentional array that has joints and realtime coordiantes but i am not sure if it is feasible to do an event handler "for selecting list items" whithin evenhandler of nui_nextskeletalframe. I wanted to do it obviously seperate but how can i return the array , if i could remember functions return one value.

    let me know if u need me to show u the code

    thanks a lot

    Wednesday, April 11, 2012 10:08 AM

Answers

  • Make the SkeletonJoint class public

    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members. Also try to Vote as Helpful

    • Marked as answer by Warrior4just Friday, May 4, 2012 10:53 PM
    • Unmarked as answer by Warrior4just Friday, May 4, 2012 10:53 PM
    • Marked as answer by Warrior4just Wednesday, May 9, 2012 9:15 PM
    Tuesday, May 1, 2012 10:05 AM
  • Hell Warrior4just,

    I am also new for Kinect C#. I tried to solve the problem here and hoped to share my solution with you. Pretty lucky, I got it working. Hope it will be helpful for you.

    First, in your XAML, make your Listbox triggering an event called SelectionChanged, XAML code like this:

    <ListBox x:Name="mcListBox" Width="100" Margin="4,40,4,129" SelectionChanged="mcListBox_SelectionChanged">

    Secondly, in the snippet code, create a public string to save the string of selected item so that it will pass valid value to DisplayJointInfo(); I make it like this:

    public string SelectedJoint;

    Thirdly, in the SelectionChange Method, I write in this way:

                

     private void mcListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
                SelectedJoint = lbi.Content.ToString();

            }

    Finally, in DisplayJointInfo method, the code is something like this:

                

    //Head
                JointType joint1 = JointType.Head;
                Joint jData1 = skeleton.Joints[joint1];
                SkeletonJoint thisSkeletonJoint = new SkeletonJoint();

                thisSkeletonJoint.X = jData1.Position.X;
                thisSkeletonJoint.Y = jData1.Position.Y;
                thisSkeletonJoint.Z = jData1.Position.Z;

                SharedJointData.Head = null;
                SharedJointData.Head = thisSkeletonJoint;

    //Create joint coordinated for others

    //Coordinates Display, Head coordinates are displayed by default in my setting.

                if (SelectedJoint == null)
                {
                    var head = SharedJointData.Head;
                    xTextBox.Text = head.X.ToString();
                    yTextBox.Text = head.Y.ToString();
                    zTextBox.Text = head.Z.ToString();
                }

                if (SelectedJoint == "Head")
                {

                    var head = SharedJointData.Head;
                    xTextBox.Text = head.X.ToString();
                    yTextBox.Text = head.Y.ToString();
                    zTextBox.Text = head.Z.ToString();
                }

    --------------------------------------------------------------------

    That's mainly all my work, which is working based on Kinect SDK V1.0. So there should be some minor difference, you just need to translate it accordingly. 

    • Marked as answer by Warrior4just Wednesday, May 16, 2012 2:03 PM
    Wednesday, May 16, 2012 2:51 AM
  • The following method is called every time you receive data from the kinect so let its implementation as follow

    void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
               SkeletonFrame skeletonFrame = e.SkeletonFrame;
                   if (IsSkeletalViewerAvailable == true )
                   {
    
                    foreach (SkeletonData skeleton in skeletonFrame.Skeletons)
                    {
                        if (SkeletonTrackingState.Tracked == skeleton.TrackingState)
                        {
                            //Call your method here
                            DisplayJointInfo(skeleton);
                        }
                     }
                    }
    }

    After that implement the following method

    private void DisplayJointInfo(Skeleton skeleton)
    {
       JointID joint1 = JointID.HipCenter;             
       Joint jData1 = skeleton.Joints[joint1];
                
       float x1 = jData1.Position.X;
       float y1 = jData1.Position.Y;
       float z1 = jData1.Position.Z;
    
       //try to format the string that will be displayed in the listbox 
       //for example 
       string listBoxItem1 = "X: " + x1.ToString() + ", Y: " + y1.ToString() + ", Z: " + z1.ToString();
    
       listbox1.items.add(listBoxItem1);
    
       //repeat the above steps for every joint
       JointID joint2 = JointID.Spine;
       Joint jData2 = skeleton.Joints[joint2];
    
       float x2 = jData2.Position.X;
       float y2 = jData2.Position.Y;
       float z2 = jData2.Position.Z;
       
       string listBoxItem2 = "X: " + x2.ToString() + ", Y: " + y2.ToString() + ", Z: " + z2.ToString();
       listbox1.items.add(listBoxItem2);
    }


    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread. Also try to Vote as Helpful

    Tuesday, April 17, 2012 8:36 AM

All replies

  • can i retrieve the coordinates from outside the nui_SkeletonFrameReady function ? how can i display the corrdinates when selecting each joint element in the listbox ?

    Any help?

    Wednesday, April 11, 2012 10:46 AM
  • Yes. You can pass the skeleton info to a method which displays the joint info and call this method in List Box Selected item event.

    Something like:

    Private void DisplayJointInfo(Skeleton _skeleton)
    {
    //display joint info
    }
    

    Hope this helps !!!

    Thanks,

    Bharat.

    Sunday, April 15, 2012 1:25 PM
  • Thanks man for ur input...

    correct me if i'm wrong, so what i do is:

    let's say i have only two joints to show my coordinates (x,y,z) for each one

    then i'd write , i think this wrong, because i am putting an even handler inside a function, but i'm writing so somebody can tell how to correct it. if possible 

    Private void DisplayJointInfo(Skeleton _skeleton) {

                void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
                {
               SkeletonFrame skeletonFrame = e.SkeletonFrame;
                   if (IsSkeletalViewerAvailable == true )
                   {

                    foreach (SkeletonData skeleton in skeletonFrame.Skeletons)
                    {
                        if (SkeletonTrackingState.Tracked == skeleton.TrackingState)
                        {

                            JointID joint1 = JointID.HipCenter;
                            JointID joint2 = JointID.Spine;
                           
                            Joint jData1 = skeleton.Joints[joint1];
                            Joint jData2 = skeleton.Joints[joint2];
                           
                            float x1 = jData1.Position.X;
                            float y1 = jData1.Position.Y;
                            float z1 = jData1.Position.Z;

                            float x2 = jData2.Position.X;
                            float y2 = jData2.Position.Y;
                            float z2 = jData2.Position.Z;

                            switch (Choix) 
                            {
                            case 1:

                            textBox1.Text = Convert.ToString(x1);
                            textBox2.Text = Convert.ToString(y1);
                            textBox3.Text = Convert.ToString(z1);
                                    break;

                            case 2:                    

                            textBox1.Text = Convert.ToString(x2);
                            textBox2.Text = Convert.ToString(y2);
                            textBox3.Text = Convert.ToString(z2);
                            break;

    }

    then write :

    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {

                ListBoxItem1(DisplayJointInfo());
            }

           or something like that...Please correct me on the syntax...i'm getting in there.

    I appreciate it man

    Monday, April 16, 2012 4:13 PM
  • The following method is called every time you receive data from the kinect so let its implementation as follow

    void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
               SkeletonFrame skeletonFrame = e.SkeletonFrame;
                   if (IsSkeletalViewerAvailable == true )
                   {
    
                    foreach (SkeletonData skeleton in skeletonFrame.Skeletons)
                    {
                        if (SkeletonTrackingState.Tracked == skeleton.TrackingState)
                        {
                            //Call your method here
                            DisplayJointInfo(skeleton);
                        }
                     }
                    }
    }

    After that implement the following method

    private void DisplayJointInfo(Skeleton skeleton)
    {
       JointID joint1 = JointID.HipCenter;             
       Joint jData1 = skeleton.Joints[joint1];
                
       float x1 = jData1.Position.X;
       float y1 = jData1.Position.Y;
       float z1 = jData1.Position.Z;
    
       //try to format the string that will be displayed in the listbox 
       //for example 
       string listBoxItem1 = "X: " + x1.ToString() + ", Y: " + y1.ToString() + ", Z: " + z1.ToString();
    
       listbox1.items.add(listBoxItem1);
    
       //repeat the above steps for every joint
       JointID joint2 = JointID.Spine;
       Joint jData2 = skeleton.Joints[joint2];
    
       float x2 = jData2.Position.X;
       float y2 = jData2.Position.Y;
       float z2 = jData2.Position.Z;
       
       string listBoxItem2 = "X: " + x2.ToString() + ", Y: " + y2.ToString() + ", Z: " + z2.ToString();
       listbox1.items.add(listBoxItem2);
    }


    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread. Also try to Vote as Helpful

    Tuesday, April 17, 2012 8:36 AM
  • Any updates

    Please feedback


    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members. Also try to Vote as Helpful

    Thursday, April 19, 2012 10:12 AM
  • Hey Mohammed Thanks a lot man,

    Sorry for not responding promptly i was working on parallel assingment, i barely had the opportunity to resume my WPF work.

    I was able at this point to integrate XNA graphic device manager inside WPF, so i could later use a 3D model or avatar that moves with the kinect corrdinates.

    But to address the above discussed Listbox items. the picture below is what i'm trying to do

    selecting each element will enable me to have the corresponding corrdinates, that is why i though about swithc case, but i'm not sure if there is a better approach, i tried to make what you have said, but it does not output anything even when taking off the textBoxes.

    Do you think switch case would do or better not.

    I appreciate it Mohammed, and sorry again for not responding earlier, i will respond promptly this time 

    Tuesday, April 24, 2012 4:51 PM
  • Switch case will be good but actually the same as using if statement

    regarding the list box do you want if you select any joint from the list box its coordinates appears in the text boxes isn't it


    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members. Also try to Vote as Helpful


    • Edited by MohamedSakr Wednesday, April 25, 2012 1:09 PM
    Wednesday, April 25, 2012 8:18 AM

  • regarding the list box do you want if you select any joint from the list box its coordinates appears in the text boxes isn't it

    yeah that's what i'm trying to do...how ??? i tried this for just two joints


            void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
            {
                foreach (SkeletonData s in e.SkeletonFrame.Skeletons)
                {
                    if (s.TrackingState == SkeletonTrackingState.Tracked)
                    {
                        skeleton = s;
                        DisplayJointInfo(skeleton);
                    }
                }
            }


            private void DisplayJointInfo(SkeletonData skeleton)
            {
                JointID joint1 = JointID.Head;
                Joint jData1 = skeleton.Joints[joint1];
                
                JointID joint2 = JointID.ShoulderCenter;
                Joint jData2 = skeleton.Joints[joint2];

                if (ListBoxItem1.IsSelected)
                {
                    float x1 = jData1.Position.X;
                    float y1 = jData1.Position.Y;
                    float z1 = jData1.Position.Z;

                    string textBox1 = x1.ToString(); string textBox2 = y1.ToString(); string textBox3 = z1.ToString();
                }

                else if (ListBoxItem2.IsSelected)
                {
                    float x2 = jData2.Position.X;
                    float y2 = jData2.Position.Y;
                    float z2 = jData2.Position.Z;

                    string textBox1 = x2.ToString(); string textBox2 = y2.ToString(); string textBox3 = z2.ToString();
                }
            }

    But it didn't show anything, do i have to do event handlers here ...or something else.

    i'm perplexed

    thank you Mohammed

    Thursday, April 26, 2012 3:53 AM
  • Try to update the code to add a new class call it SkeletonJoint

    public Class SkeletonJoint
    {
        public JointId JointId;
        public float X;
        public float Y;
        public float Z;
    }

    And create a static class and call it SharedJointData to hold the latest joint information

    public static class SharedJointData
    {
        public static SkeletonJoint Head;
        public static SkeletonJoint HandRight;
        public static SkeletonJoint HandLeft;
        public static SkeletonJoint ShoulderCenter;
        //Create for all the joint of the skeleton
    }

    And update the DisplayJointInfo method to be the following

    private void DisplayJointInfo(Skeleton skeleton)
    {
       JointID joint1 = JointID.HipCenter;             
       Joint jData1 = skeleton.Joints[joint1];
        
       SkeletonJoint thisSkeletonJoint = new SkeletonJoint(); 
       thisSkeletonJoint.JointId = joint1;
       thisSkeletonJoint.X = jData1.Position.X;
       thisSkeletonJoint.Y = jData1.Position.Y;
       thisSkeletonJoint.Z = jData1.Position.Z;
       
       SharedJointData.HipCenter = null;
       SharedJointData.HipCenter = thisSkeletonJoint;
       
       //repeat all the above steps for all the Joints
    }

    after that in the selected index changed event of the listbox  get the joint that is selected from the list box from the static class SharedJointData

    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members. Also try to Vote as Helpful

    • Proposed as answer by MohamedSakr Thursday, April 26, 2012 5:35 PM
    Thursday, April 26, 2012 5:35 PM

  • Thank you Mohammed, 

    I created the two classes u have suggested. I tried to display the resut for each joint point that i defined "4 joints so to test" but nothing is displayed .

    u said "after that in the selected index changed event of the listbox  get the joint that is selected from the list box from the static class SharedJointData"

    this is the actual part that i found an issue with .

    Regards

    Monday, April 30, 2012 3:41 AM
  • Yes exactly

    get the the joint name that is selected after that compare that selected joint from the listbox to the SharedJointData

    something like this

    if (listbox.selectedItem.ToString() == "HipCenter")
    {
     var hipCenter = SharedJointData.HipCenter;
     xTextBox.Text = hipCenter.X;
     yTextBox.Text = hipCenter.Y;
     zTextBox.Text = hipCenter.Z;
    
     //and so on for the remaining joints
    }


    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members. Also try to Vote as Helpful

    Monday, April 30, 2012 8:40 AM

  • Mohammed, hi doing bro

    I implemented thus far what u have suggested. There is an error. i still ran the executable but there is no real-time reading, i get the skeleton just fine like before, but no reading.

    there is this error i've never seen about the fact that SkeletonJoint class is less accessible than SharedJointData class

    Error 1 Accessibilité incohérente : le type de champ 'WpfHostedXna.SkeletonJoint' est moins accessible que le champ 'WpfHostedXna.SharedJointData.Head'

    humm, what could be the problem ?

    Regards


    Tuesday, May 1, 2012 1:14 AM
  • Make the SkeletonJoint class public

    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members. Also try to Vote as Helpful

    • Marked as answer by Warrior4just Friday, May 4, 2012 10:53 PM
    • Unmarked as answer by Warrior4just Friday, May 4, 2012 10:53 PM
    • Marked as answer by Warrior4just Wednesday, May 9, 2012 9:15 PM
    Tuesday, May 1, 2012 10:05 AM
  • Hi doing Mohammed, tnks man

    making the class public did fix the reading of the values only when take the if statement, in another word:

    just 

    var hipCenter = SharedJointData.HipCenter; xTextBox.Text = hipCenter.X; yTextBox.Text = hipCenter.Y; zTextBox.Text = hipCenter.Z;

    when i do the if statement for few more joints

    it complains about listbox.selectedItem.

    the error is:

    'WpfHostedXna.MainWindow' ne contient pas une définition pour 'listBox1_SelectionChanged' et aucune méthode d'extension 'listBox1_SelectionChanged' acceptant un premier argument de type 'WpfHostedXna.MainWindow' n'a été trouvée (une directive using ou une référence d'assembly est-elle manquante ?)

    That just as far how i got...that's with ur help. i did swithc case for two joint it gave the same problem , "scratch my head" ???

    Regards

    Wednesday, May 2, 2012 2:52 PM
  • Great to hear from you that your problem did fix the issue

    could you please mark my last reply as answer if it helped you


    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members. Also try to Vote as Helpful

    Friday, May 4, 2012 9:23 PM
  • Hey Mohammed i will definetly do so, though it appears that i wasn't clear about something just above, yes indeed making the class public fixed the problem, but when i use the if statement to see the other values from the joints, there is an error in listbox.selectedItem.

    when i do this

    if (listbox.selectedItem.ToString() == "HipCenter") { var hipCenter = SharedJointData.HipCenter; xTextBox.Text = hipCenter.X; yTextBox.Text = hipCenter.Y; zTextBox.Text = hipCenter.Z; //and so on for the remaining joints }

    lt complains about listbox.selectedItem.ToString()


    Friday, May 4, 2012 10:53 PM
  • what is the error

    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members. Also try to Vote as Helpful

    Friday, May 4, 2012 11:27 PM
  • Mohammed, hi doing bro

    The error is:


    'WpfHostedXna.MainWindow' ne contient pas une définition pour 'listBox1_SelectionChanged' et  aucune méthode  d'extension  'listBox1_SelectionChanged' acceptant un premier argument de type 'WpfHostedXna.MainWindow' n'a été trouvée (une directive using ou une référence  d'assembly  est-elle manquante ?)


    in another word:

    'WpfHostedXna.MainWindow' does not contain definition for 'listBox1_SelectionChanged'  and there is no extention method 'listBox1_SelectionChanged' accepting first argument of type 'WpfHostedXna.MainWindow'  is found (a directive using or  an assembly reference is missing ?)


    Saturday, May 5, 2012 6:18 PM
  • Please tell me the exact scenario in order to be able to help you

    Thanks,
    MOHAMED A. SAKR | Software Development Lead Engineer | EgyptNetwork
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members. Also try to Vote as Helpful

    Thursday, May 10, 2012 5:42 PM
  • Hello Mohammed

    my problem is the following: I have a listbox with 20 elements "joints", i want to display XYZ corrdinate for each joint in three textboxes. So everytime i select an element "joint" from the listbox, i get their corresponding XYZ corrdinates displayed in those said textboxes,  just three textboxes.

    and it is not working with previous code. what is working only the first joint in the list wihtout any if statements. But once i put the if statement....i execute, then the error comes out as an exception messagebox in the edior:

    WpfHostedXna.MainWindow' does not contain definition for 'listBox1_SelectionChanged'  and there is no extention method 'listBox1_SelectionChanged' accepting first argument of type 'WpfHostedXna.MainWindow'  is found (a directive using or  an assembly reference is missing ?)

    Hope i was clear about the problem. Thanks Bro

    Saturday, May 12, 2012 2:15 PM
  • Hell Warrior4just,

    I am also new for Kinect C#. I tried to solve the problem here and hoped to share my solution with you. Pretty lucky, I got it working. Hope it will be helpful for you.

    First, in your XAML, make your Listbox triggering an event called SelectionChanged, XAML code like this:

    <ListBox x:Name="mcListBox" Width="100" Margin="4,40,4,129" SelectionChanged="mcListBox_SelectionChanged">

    Secondly, in the snippet code, create a public string to save the string of selected item so that it will pass valid value to DisplayJointInfo(); I make it like this:

    public string SelectedJoint;

    Thirdly, in the SelectionChange Method, I write in this way:

                

     private void mcListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
                SelectedJoint = lbi.Content.ToString();

            }

    Finally, in DisplayJointInfo method, the code is something like this:

                

    //Head
                JointType joint1 = JointType.Head;
                Joint jData1 = skeleton.Joints[joint1];
                SkeletonJoint thisSkeletonJoint = new SkeletonJoint();

                thisSkeletonJoint.X = jData1.Position.X;
                thisSkeletonJoint.Y = jData1.Position.Y;
                thisSkeletonJoint.Z = jData1.Position.Z;

                SharedJointData.Head = null;
                SharedJointData.Head = thisSkeletonJoint;

    //Create joint coordinated for others

    //Coordinates Display, Head coordinates are displayed by default in my setting.

                if (SelectedJoint == null)
                {
                    var head = SharedJointData.Head;
                    xTextBox.Text = head.X.ToString();
                    yTextBox.Text = head.Y.ToString();
                    zTextBox.Text = head.Z.ToString();
                }

                if (SelectedJoint == "Head")
                {

                    var head = SharedJointData.Head;
                    xTextBox.Text = head.X.ToString();
                    yTextBox.Text = head.Y.ToString();
                    zTextBox.Text = head.Z.ToString();
                }

    --------------------------------------------------------------------

    That's mainly all my work, which is working based on Kinect SDK V1.0. So there should be some minor difference, you just need to translate it accordingly. 

    • Marked as answer by Warrior4just Wednesday, May 16, 2012 2:03 PM
    Wednesday, May 16, 2012 2:51 AM
  • Hey Trtian

    Dude, that was awesome....4 words: One Big Sincere Thankyou

    Wednesday, May 16, 2012 2:02 PM