wpf - 如何将方法绑定(bind)到 HoverButton?

标签 wpf binding kinect icommand

我已经找了好几个小时了,但找不到任何有用的东西。任何帮助表示赞赏!

我正在使用 WPF 以及 Coding4Fun 工具包和 MVVM 模式编写 Kinect 应用程序。

我想将所有与 kinect 相关的逻辑放入 ViewModel 中,并将这些方法绑定(bind)到 HoverButton(可在 C4F 工具包中找到)。 普通按钮具有“Command”属性,但 HoverButton 没有。

简而言之:

我想将 HoverButton 的单击事件绑定(bind)到 ViewModel 中的方法。

我的 XAML:

<Window x:Class="KinectTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:fun="clr-namespace:Coding4Fun.Kinect.Wpf.Controls;assembly=Coding4Fun.Kinect.Wpf" Title="MainWindow" Height="350" Width="525"
    Loaded="WindowLoaded"
    Closed="WindowClosed"
    Cursor="None"
    >
<Grid Name="MainGrid" MouseMove="GridHoverMouseMove" DataContext="_viewModel">

    <Canvas Name="SkeletonCanvas" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Black">
        <fun:HoverButton Name="KinectUp" ImageSource="/Images/blue_glass.png" ActiveImageSource="/Images/blue_glass.png" ImageSize="100" Canvas.Top="26" TimeInterval="1000">

        </fun:HoverButton>
        <fun:HoverButton Name="KinectDown" ImageSource="/Images/red_glass.png" ActiveImageSource="/Images/red_glass.png" ImageSize="100" Canvas.Bottom="26" TimeInterval="1000"/>

    </Canvas>
    <Image Name="ColorImage" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="120" Height="120"></Image>
            <TextBlock Name="Notification" Foreground="White" FontSize="50" VerticalAlignment="Top" HorizontalAlignment="Stretch" TextAlignment="Center" Text="{Binding Path=Notification, Mode=TwoWay}"></TextBlock>
    <Canvas Name="CanvMouse" Background="Transparent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <Image Name="imgMouse" Width="70" Source="/Images/handround_green.png"></Image>
    </Canvas>
</Grid>
</Window>

我的 View 模型:

internal class MainViewModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    public ICommand KinectUpClick { get; private set; }
    public ICommand KinectDownClick { get; private set; }

    private string _notification = "Hello";
    public SensorHelper SensorHelper { get; set; }

    public string Notification
    {
        get { return _notification; }
        set
        {
            _notification = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Notification"));
        }
    }



    public MainViewModel()
    {
        KinectUpClick = new RelayCommand(PerformKinectUpClick, CanKinectUpClick);
        KinectDownClick = new RelayCommand(PerformKinectDownClick, CanKinectDownClick);

    }

    private bool CanKinectUpClick(object parameter)
    {
        return SensorHelper.CanMoveUp;
    }

    private bool CanKinectDownClick(object parameter)
    {
        return SensorHelper.CanMoveDown;
    }

    private void PerformKinectUpClick(object parameter)
    {

        ThreadPool.QueueUserWorkItem((o) =>
        {
            Notification = "Kinect goes up!";
            SensorHelper.MoveAngle(5);
            Notification = "Kinect ready...";
        });
    }

    private void PerformKinectDownClick(object parameter)
    {
        ThreadPool.QueueUserWorkItem((o) =>
        {
            Notification = "Kinect goes down!";
            SensorHelper.MoveAngle(-5);
            Notification = "Kinect ready...";
        });
        ;
    }
}
}

我的代码隐藏:

public partial class MainWindow : Window
{
    private readonly MainViewModel _viewModel;
    private SensorHelper _sensorHelper;

    public MainWindow()
    {
        InitializeComponent();
        _viewModel = new MainViewModel();
        MainGrid.DataContext = _viewModel;
    }

    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        try
        {
            _sensorHelper = new SensorHelper();
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message);
        }
        _viewModel.SensorHelper = _sensorHelper;
        _sensorHelper.InitializeSkeleton(ref SkeletonCanvas);
        //_sensorHelper.CaptureMouse(CanvMouse);
        _sensorHelper.InitializeColorFrame(ColorImage);
        _sensorHelper.StartKinect();
    }



    private void WindowClosed(object sender, EventArgs eventArgs)
    {
        _sensorHelper.StopKinect();
    }

    private void GridHoverMouseMove(object sender, MouseEventArgs e)
    {
        imgMouse.SetValue(Canvas.LeftProperty, e.GetPosition(CanvMouse).X - imgMouse.ActualWidth/2);
        imgMouse.SetValue(Canvas.TopProperty, e.GetPosition(CanvMouse).Y - imgMouse.ActualHeight/2);

        MouseHelper.CheckButton(KinectUp, imgMouse);
        MouseHelper.CheckButton(KinectDown, imgMouse);
    }
}

最佳答案

好吧,非常简单,您要做的就是使用 EventToCommand 将 ICommand/Command 绑定(bind)到事件,EventToCommand 可以在 MVVMLight 工具包中找到。

您也可以使用 Blend 来实现这一点

简单示例:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
    x:Class="TestProject.Window5"
    x:Name="Window"
    Title="Window5"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot">
        <Button Content="Button" HorizontalAlignment="Left" Height="69" Margin="92,117,0,0" VerticalAlignment="Top" Width="206">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Kinect.MyCommand, Source={StaticResource Locator}}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    </Grid>
</Window>

关于wpf - 如何将方法绑定(bind)到 HoverButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10333417/

相关文章:

c# - WPF 自定义控件默认数据绑定(bind)

c# - wpf 绑定(bind)到另一个 xaml 文件中的元素

c# - 为什么要使用MVVM? [复制]

c# - 字典到 ListView TwoWay 绑定(bind) - 可能吗?

xamarin - 条目绑定(bind)到 int?以 Xamarin 形式

binding - Knockout.js - 启用范围绑定(bind)

C++ kinect 和 openni : convert depth to real world

c# - DataTemplate 内的 DataTemplate - ListBox 内的 ListBox

graphics - 使用OpenCV查找变换矩阵

c# - C# : Translate forward motion to up motion on the screen 中的 KINECT