Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to bind method to a HoverButton?
    text
    copied!<p>I've been looking for hours but I can't find anything useful. Any help appreciated!</p> <p>I'm writing a Kinect application using WPF with the Coding4Fun toolkit and the MVVM pattern.</p> <p>I'd like to put all my kinect related logic in my ViewModel and bind those methods to a HoverButton (found in the C4F toolkit). A normal button had the 'Command' property, However the HoverButton does not.</p> <p>So in short:</p> <p>I want to bind the click event of a HoverButton to a method in my ViewModel.</p> <p><strong>My XAML:</strong></p> <pre><code>&lt;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" &gt; &lt;Grid Name="MainGrid" MouseMove="GridHoverMouseMove" DataContext="_viewModel"&gt; &lt;Canvas Name="SkeletonCanvas" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Black"&gt; &lt;fun:HoverButton Name="KinectUp" ImageSource="/Images/blue_glass.png" ActiveImageSource="/Images/blue_glass.png" ImageSize="100" Canvas.Top="26" TimeInterval="1000"&gt; &lt;/fun:HoverButton&gt; &lt;fun:HoverButton Name="KinectDown" ImageSource="/Images/red_glass.png" ActiveImageSource="/Images/red_glass.png" ImageSize="100" Canvas.Bottom="26" TimeInterval="1000"/&gt; &lt;/Canvas&gt; &lt;Image Name="ColorImage" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="120" Height="120"&gt;&lt;/Image&gt; &lt;TextBlock Name="Notification" Foreground="White" FontSize="50" VerticalAlignment="Top" HorizontalAlignment="Stretch" TextAlignment="Center" Text="{Binding Path=Notification, Mode=TwoWay}"&gt;&lt;/TextBlock&gt; &lt;Canvas Name="CanvMouse" Background="Transparent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"&gt; &lt;Image Name="imgMouse" Width="70" Source="/Images/handround_green.png"&gt;&lt;/Image&gt; &lt;/Canvas&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>My ViewModel:</strong></p> <pre><code>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) =&gt; { Notification = "Kinect goes up!"; SensorHelper.MoveAngle(5); Notification = "Kinect ready..."; }); } private void PerformKinectDownClick(object parameter) { ThreadPool.QueueUserWorkItem((o) =&gt; { Notification = "Kinect goes down!"; SensorHelper.MoveAngle(-5); Notification = "Kinect ready..."; }); ; } } } </code></pre> <p><strong>My Code-behind:</strong></p> <pre><code>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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload