Window_Loaded中的DataContext

时间:2011-11-19 06:16:39

标签: wpf events datacontext loaded

我是WPF的新手。我目前正在使用Kinect SDK执行代码检测关节坐标,并在WPF中的简单文本框中显示。检测关节的代码位于私有void Window_Loaded(对象发送方,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方法中。任何解决方案?

1 个答案:

答案 0 :(得分:1)

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

然后,如果您更改了对象的值,它将获取更改的值...也 只需在Window Constructor中设置DataContext=myCordinate,然后在窗口类中将我的cordinate变为变量。

    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 =“您喜欢什么”的值,无论如何它都会通知您的视图相应的属性已更改..

我在这里提出完整的解决方案

我的坐标类

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

}

是的,我测试的项目......正在运行

这可能会有所帮助:)