1

我是 WPF 和 c# 的初学者。我正在尝试使用 Kinect for Windows 创建一个应用程序。我的 Kinect.cs 中有一个 kinectsensorchooser,它控制 MainWindow.xaml 中的 SensorChooser 但我不知道如何控制它。
我的代码如下:

主窗口.xaml

<Canvas>  
<k:KinectSensorChooserUI KinectSensorChooser="{Binding SCkinectSensorChooser} "Name="sensorChooserCP"/>  
<k:KinectUserViewer k:KinectRegion.KinectRegion="{Binding kinectRegionCP}"/>  
<k:KinectRegion Name="kinectRegionCP" KinectSensor="{Binding ElementName=SCkinectsensor}">  
<Grid>  
some kinect tile buttons come in here...  
</Grid>  
</k:KinectRegion>  
</Canvas>  

Kinect.cs

public KinectSensor SCkinectsensor;  
public KinectSensorChooser SCkinectSensorChooser;  

这两个对象的值将在程序执行期间动态设置。我希望这些更改反映在 MainWindow

应用程序.xaml

<Application x:Class="Kinect.App"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
StartupUri="MainWindow.xaml"  
xmlns:local="clr-namespace:Kinect">  
<Application.Resources>  
<local:Kinect x:Key="Kinect" />  
</Application.Resources>

我做错了什么,代码没有按我的意愿响应......我该怎么办?我需要帮助

4

1 回答 1

1

DataContext您是否在主窗口中设置您的?如果没有,你Binding将什么也不做。

主窗口.xaml

<Window x:Class="Kinect.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{StaticResource Kinect}">
...
</Window>

另一件需要注意的是,在 WPF 中,您只能绑定到properties

Kinect.cs

public KinectSensor SCkinectsensor { get; private set; }
public KinectSensorChooser SCkinectSensorChooser { get; private set; }

如果您希望这些属性在构造函数之外更改,则需要实现此类INotifyPropertyChanged,并且您的属性将如下所示:

private KinectSensor kinectSensor;
public KinectSensor SCkinectSensor
{
    get { return kinectSensor; }
    set 
    {
        kinectSensor = value;
        PropertyChanged(this, new PropertyChangedEventArgs("SCkinectSensor");
    }
}
于 2013-06-28T15:49:51.970 回答