C# WPF Binding Sample Complete Code

[Back to the SDK Overview]

App.xaml

<!--
COPYRIGHT 2018-2020 - PROPERTY OF TOBII AB
2018-2020 TOBII AB - KARLSROVAGEN 2D, DANDERYD 182 53, SWEDEN - All Rights Reserved.
NOTICE: All information contained herein is, and remains, the property of Tobii AB and its suppliers, if any.
The intellectual and technical concepts contained herein are proprietary to Tobii AB and its suppliers and may be
covered by U.S.and Foreign Patents, patent applications, and are protected by trade secret or copyright law.
Dissemination of this information or reproduction of this material is strictly forbidden unless prior written
permission is obtained from Tobii AB.
-->
<Application x:Class="Tobii.InteractionLib.Wpf.SampleApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Tobii.InteractionLib.Wpf.SampleApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

App.xaml.cs

/*
COPYRIGHT 2018-2020 - PROPERTY OF TOBII AB
-------------------------------------
2018-2020 TOBII AB - KARLSROVAGEN 2D, DANDERYD 182 53, SWEDEN - All Rights Reserved.
NOTICE: All information contained herein is, and remains, the property of Tobii AB and its suppliers, if any.
The intellectual and technical concepts contained herein are proprietary to Tobii AB and its suppliers and may be
covered by U.S.and Foreign Patents, patent applications, and are protected by trade secret or copyright law.
Dissemination of this information or reproduction of this material is strictly forbidden unless prior written
permission is obtained from Tobii AB.
*/
using System;
using System.Windows;
namespace Tobii.InteractionLib.Wpf.SampleApp
{
public partial class App : Application
{
// Exposes the Interaction Library WPF host. Makes it possible for windows (like MainWindow in this sample)
// to add themselves to the Interaction Library WPF binding of the host.
public Tobii.InteractionLib.Wpf.InteractionLibWpfHost IntlibWpf { get; private set; }
// This function is called when the application loads very first time. On startup, we instantiate the
// Interaction Library WPF host and assign it to the IntlibWpf property which can later be used by other windows.
protected override void OnStartup(StartupEventArgs e) => IntlibWpf = new Tobii.InteractionLib.Wpf.InteractionLibWpfHost();
// This function is called when the application exits.
protected override void OnExit(ExitEventArgs e)
{
// Cleans up the Interaction Library WPF host instance before the application exits.
IntlibWpf?.Dispose();
base.OnExit(e);
}
}
}

MainWindow.xaml

<!--
COPYRIGHT 2018-2020 - PROPERTY OF TOBII AB
2018-2020 TOBII AB - KARLSROVAGEN 2D, DANDERYD 182 53, SWEDEN - All Rights Reserved.
NOTICE: All information contained herein is, and remains, the property of Tobii AB and its suppliers, if any.
The intellectual and technical concepts contained herein are proprietary to Tobii AB and its suppliers and may be
covered by U.S.and Foreign Patents, patent applications, and are protected by trade secret or copyright law.
Dissemination of this information or reproduction of this material is strictly forbidden unless prior written
permission is obtained from Tobii AB.
-->
<!-- This is the MainWindow UI where we add three rectangle controls to the window.
The rectangles should trigger change color animation from LightSkyBlue to DarkRed
when Gaze enter action is detected, and change color back from DarkRed to LightSkyBlue
when Gaze exit action is detected. -->
<Window x:Class="Tobii.InteractionLib.Wpf.SampleApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wpf="clr-namespace:Tobii.InteractionLib.Wpf;assembly=tobii_interaction_lib_wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<Style x:Key="RectangleWithGazeAwareAnimation" TargetType="Rectangle">
<Setter Property="Fill" Value="LightSkyBlue" />
<Setter Property="Margin" Value="40, 50" />
<Setter Property="wpf:Behaviors.IsGazeAware" Value="True"></Setter>
<Style.Triggers>
<Trigger Property="wpf:Behaviors.HasGaze" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)"
From="LightSkyBlue" To="DarkRed" Duration="0:0:.2" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)"
From="DarkRed" To="LightSkyBlue" Duration="0:0:.2" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Window.Resources>
<UniformGrid Columns="3">
<Rectangle x:Name="first" Style="{StaticResource RectangleWithGazeAwareAnimation}" />
<Rectangle x:Name="second" Style="{StaticResource RectangleWithGazeAwareAnimation}" />
<Rectangle x:Name="third" Style="{StaticResource RectangleWithGazeAwareAnimation}" />
</UniformGrid>
</Window>

MainWindow.xaml.cs

/*
COPYRIGHT 2018-2020 - PROPERTY OF TOBII AB
-------------------------------------
2018-2020 TOBII AB - KARLSROVAGEN 2D, DANDERYD 182 53, SWEDEN - All Rights Reserved.
NOTICE: All information contained herein is, and remains, the property of Tobii AB and its suppliers, if any.
The intellectual and technical concepts contained herein are proprietary to Tobii AB and its suppliers and may be
covered by U.S.and Foreign Patents, patent applications, and are protected by trade secret or copyright law.
Dissemination of this information or reproduction of this material is strictly forbidden unless prior written
permission is obtained from Tobii AB.
*/
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace Tobii.InteractionLib.Wpf.SampleApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
// Here we attach the current window to the already created instance of Interaction Library WpfBinding host.
// Note, we are doing this before calling InitializeComponent so that WpfBinding has proper values
// as soon as the window is loaded, without waiting for NotifyPropertyChanged method to be called.
((App)Application.Current).IntlibWpf?.WpfBinding?.AddWindow(this);
InitializeComponent();
}
}
}