title: Setting an XAML Window always on top (but no TopMost property)
body: <p>I am developing an application based on OptiTrack SDK (from NaturalPoint). I need to run the application window as "Always on Top". The window is designed in XAML and is controled in the class "CameraView" but it does not seem to include a "TopMost" property or equivalent. Attached are the code of "CameraView.xaml.cs" and the code of "CameraView.xaml" that are part of OptiTrack SDK (NaturalPoint) called "Single_Camera_CSharp_.NET_3.0".</p>
  
  <p>One could expect the class CameraView to contain properties or members to set the position of the window on the screen or to set it to TopMost but as far as searched I found nothing. I wonder what I should do.</p>
  
  <p>Thank you,
  Brian</p>
  
  <p>================</p>
  
  <h1>"CameraView.xaml.cs"</h1>
  
  <pre><code>using System;
  using System.IO;
  using System.Net;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Media;
  using System.Windows.Media.Animation;
  using System.Windows.Navigation;
  using System.Drawing.Imaging;
  using System.Drawing.Drawing2D;
  using System.Windows.Threading;
  
  namespace TestProject
  {
  public partial class CameraView
  {
  private const int NP_OPTION_OBJECT_COLOR_OPTION = 3;
  private const int NP_OPTION_VIDEO_TYPE = 48;
  private const int NP_OPTION_NUMERIC_DISPLAY_ON = 71;
  private const int NP_OPTION_NUMERIC_DISPLAY_OFF = 72;
  private const int NP_OPTION_FETCH_RLE = 73;
  private const int NP_OPTION_FETCH_GRAYSCALE = 74;
  private const int NP_OPTION_FRAME_DECIMATION = 52;
  private const int NP_OPTION_INTENSITY = 50;
  private const int NP_OPTION_SEND_EMPTY_FRAMES = 41;
  private const int NP_OPTION_THRESHOLD = 5;
  private const int NP_OPTION_EXPOSURE = 46;
  private const int NP_OPTION_SEND_FRAME_MASK = 73;
  private const int NP_OPTION_TEXT_OVERLAY_OPTION = 74;
  // public delegate void OnCameraViewCreate(CameraView camera);
  // public static OnCameraViewCreate onCameraViewCreate;
  private System.Drawing.Bitmap raw = new System.Drawing.Bitmap(353, 288, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  private int mFrameCounter;
  private int mDisplayCounter;
  private DispatcherTimer timer1 = new DispatcherTimer();
  private bool mVideoFrameAvailable = false;
  private int mNumeric = -1;
  private bool mGreyscale = false;
  private bool mOverlay = true;
  public CameraView()
  {
  this.InitializeComponent();
  
  timer1.Interval = new TimeSpan(0, 0, 0, 0, 10);
  timer1.Tick += new EventHandler(timer1_Tick);
  }
  
  public int Numeric
  {
  get { return mNumeric; }
  set
  {
  mNumeric = value % 100;
  if (mNumeric &gt;= 0)
  {
  if (Camera != null)
  Camera.SetOption(NP_OPTION_NUMERIC_DISPLAY_ON, value % 100);
  }
  }
  }
  
  private bool CameraRunning = false;
  private OptiTrack.NPCamera mCamera;
  public OptiTrack.NPCamera Camera
  {
  get { return mCamera; }
  set
  {
  if (mCamera == value) return; //== Don't do anything if you're assigning the same camera ==
  
  if (mCamera != null)
  {
  //== Shut the selected camera down ==&lt;&lt;
  
  if (CameraRunning)
  {
  CameraRunning = false;
  mCamera.Stop();
  mCamera.FrameAvailable -= FrameAvailable;
  }
  }
  
  mCamera = value;
  
  if (mCamera == null)
  {
  mNumeric = -1;
  }
  else
  {
  serialLabel.Content = "Camera "+mCamera.SerialNumber.ToString(); //mNumeric.ToString();
  }
  }
  }
  
  private void FrameAvailable(OptiTrack.NPCamera Camera)
  {
  mFrameCounter++;
  
  try
  {
  OptiTrack.NPCameraFrame frame = Camera.GetFrame(0);
  int id = frame.Id;
  if (CameraRunning)
  {
  GetFrameData(Camera, frame);
  }
  
  frame.Free();
  }
  catch (Exception)
  {
  int r = 0;
  r++;
  }
  }
  private void GetFrameData(OptiTrack.NPCamera camera, OptiTrack.NPCameraFrame frame)
  {
  BitmapData bmData = raw.LockBits(new System.Drawing.Rectangle(0, 0, raw.Width, raw.Height),
  ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  
  int stride = bmData.Stride;
  System.IntPtr bufferPtr = bmData.Scan0;
  unsafe
  {
  byte* buffer = (byte*)(void*)bufferPtr;
  camera.GetFrameImage(frame, bmData.Width, bmData.Height, bmData.Stride, 32, ref buffer[0]);
  }
  raw.UnlockBits(bmData);
  mVideoFrameAvailable = true;
  }
  
  private void timer1_Tick(object sender, EventArgs e)
  {
  if (CameraRunning &amp;&amp; mVideoFrameAvailable)
  {
  mVideoFrameAvailable = false;
  cameraImage.Source = Img(raw);
  mDisplayCounter++;
  }
  }
  
  private System.Windows.Media.ImageSource Img(System.Drawing.Bitmap img)
  {
  System.Drawing.Imaging.BitmapData bmData = img.LockBits(new System.Drawing.Rectangle(0, 0, img.Width, img.Height),
  System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
  
  System.Windows.Media.Imaging.BitmapSource bitmap = System.Windows.Media.Imaging.BitmapSource.Create(
  img.Width, img.Height, 96, 96, PixelFormats.Bgra32,
  System.Windows.Media.Imaging.BitmapPalettes.WebPalette,
  bmData.Scan0, bmData.Stride * bmData.Height, bmData.Stride);
  img.UnlockBits(bmData);
  
  return bitmap;
  }
  private void startStopButton_Click(object sender, RoutedEventArgs e)
  {
  if (CameraRunning)
  StopCamera();
  else
  StartCamera();
  }
  
  public void StartCamera()
  {
  if (Camera != null)
  {
  mFrameCounter = 0;
  mDisplayCounter = 0;
  
  Camera.FrameAvailable += FrameAvailable;
  
  Camera.SetOption(NP_OPTION_VIDEO_TYPE, 0);
  Camera.SetOption(NP_OPTION_FRAME_DECIMATION, 1);
  
  Camera.SetOption(NP_OPTION_INTENSITY, 0);
  Camera.SetOption(NP_OPTION_EXPOSURE, 10);
  Camera.SetOption(NP_OPTION_THRESHOLD, 50);
  Camera.SetOption(NP_OPTION_OBJECT_COLOR_OPTION, 0);
  SetOverlayOption();
  SetGreyscaleOption();
  
  timer1.Start();
  Camera.Start();
  CameraRunning = true;
  this.Numeric = mNumeric;
  
  startStopButton.Content = "Stop Camera";
  }
  }
  private void SetGreyscaleOption()
  {
  if(mGreyscale)
  Camera.SetOption(NP_OPTION_VIDEO_TYPE, 1);
  else
  Camera.SetOption(NP_OPTION_VIDEO_TYPE, 0);
  }
  private void SetOverlayOption()
  {
  if(mOverlay)
  Camera.SetOption(NP_OPTION_TEXT_OVERLAY_OPTION, 255);
  else
  Camera.SetOption(NP_OPTION_TEXT_OVERLAY_OPTION, 0);
  }
  
  public void StopCamera()
  {
  if (Camera != null)
  {
  Camera.Stop();
  timer1.Stop();
  CameraRunning = false;
  Camera.FrameAvailable -= FrameAvailable;
  Camera.SetOption(NP_OPTION_NUMERIC_DISPLAY_OFF, 0);
  startStopButton.Content = "Start Camera";
  }
  }
  
  private void greyscaleButton_Click(object sender, RoutedEventArgs e)
  {
  if(mGreyscale)
  mGreyscale = false;
  else
  mGreyscale = true;
  SetGreyscaleOption();
  }
  
  private void OverlayButton_Click(object sender, RoutedEventArgs e)
  {
  if(mOverlay)
  mOverlay = false;
  else
  mOverlay = true;
  SetOverlayOption();
  }
  
  private void exposureSlider_ValueChanged(object sender, RoutedEventArgs e)
  {
  if (mCamera!=null)
  {
  mCamera.SetOption(NP_OPTION_EXPOSURE, (int) this.exposureSlider.Value);
  }
  }
  
  private void thresholdSlider_ValueChanged(object sender, RoutedEventArgs e)
  {
  if (mCamera != null)
  {
  mCamera.SetOption(NP_OPTION_THRESHOLD, (int)this.thresholdSlider.Value);
  }
  }
  
  private void optionsButton_Click(object sender, RoutedEventArgs e)
  {
  if (!propertyPanel.IsVisible)
  propertyPanel.Visibility = Visibility.Visible;
  else
  propertyPanel.Visibility = Visibility.Collapsed;
  }
  }
  }
  </code></pre>
  
  <p>================</p>
  
  <h1>"CameraView.xaml"</h1>
  
  <pre><code>&lt;UserControl
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   x:Class="TestProject.CameraView"
   x:Name="CameraView1"
   Width="Auto" Height="Auto"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2006" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      mc:Ignorable="d"
   &gt;
  
   &lt;Grid x:Name="LayoutRoot" Width="Auto" Height="Auto"&gt;
    &lt;Grid.Background&gt;
     &lt;x:Null/&gt;
    &lt;/Grid.Background&gt;
    &lt;Grid.ColumnDefinitions&gt;
     &lt;ColumnDefinition Width="*"/&gt;
     &lt;ColumnDefinition Width="113.904"/&gt;
    &lt;/Grid.ColumnDefinitions&gt;
    &lt;Grid.RowDefinitions&gt;
     &lt;RowDefinition Height="26.667"/&gt;
     &lt;RowDefinition Height="*"/&gt;
    &lt;/Grid.RowDefinitions&gt;
    &lt;Grid Margin="0,0,0,0" Grid.ColumnSpan="2"&gt;
     &lt;Rectangle RadiusX="1.25" RadiusY="1.25" Margin="0,0,0,0" VerticalAlignment="Stretch"&gt;
      &lt;Rectangle.Fill&gt;
       &lt;LinearGradientBrush EndPoint="0.492,0.149" StartPoint="0.492,0.843"&gt;
        &lt;GradientStop Color="#FF000000" Offset="0"/&gt;
        &lt;GradientStop Color="#FF23283F" Offset="1"/&gt;
       &lt;/LinearGradientBrush&gt;
      &lt;/Rectangle.Fill&gt;
      &lt;Rectangle.Stroke&gt;
       &lt;LinearGradientBrush EndPoint="0.291,-4.231" StartPoint="1.668,18.025"&gt;
        &lt;GradientStop Color="#FF000000" Offset="0"/&gt;
        &lt;GradientStop Color="#FFFFFFFF" Offset="1"/&gt;
       &lt;/LinearGradientBrush&gt;
      &lt;/Rectangle.Stroke&gt;
     &lt;/Rectangle&gt;
     &lt;Rectangle RadiusX="3.333" RadiusY="3.333" Opacity="0.13" Margin="0,0,0,13"&gt;
      &lt;Rectangle.Fill&gt;
       &lt;SolidColorBrush Color="#FFFFFFFF"/&gt;
      &lt;/Rectangle.Fill&gt;
      &lt;Rectangle.Stroke&gt;
       &lt;x:Null/&gt;
      &lt;/Rectangle.Stroke&gt;
     &lt;/Rectangle&gt;
    &lt;/Grid&gt;
    &lt;Image Margin="0,0,0,0" x:Name="cameraImage" Grid.ColumnSpan="2" Grid.Row="1"/&gt;
    &lt;Label x:Name="serialLabel" FontSize="10" FontWeight="Bold" Foreground="#FFDEDADA" Content="Camera 10024" HorizontalAlignment="Right" Margin="0,0,4,0" VerticalAlignment="Top" Grid.Column="1"&gt;
     &lt;Label.BitmapEffect&gt;
      &lt;OuterGlowBitmapEffect GlowColor="#FF000000" GlowSize="4" Opacity="0.7"/&gt;
     &lt;/Label.BitmapEffect&gt;
    &lt;/Label&gt;
    &lt;WrapPanel Margin="3,3,3,3"&gt;
     &lt;Button HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="startStopButton" Width="100" Height="Auto" BorderThickness="0,0,0,0" Content="Start Camera" Click="startStopButton_Click"/&gt;
     &lt;Button x:Name="optionsButton" Width="61.474" Height="Auto" BorderThickness="0,0,0,0" Content="Options" Click="optionsButton_Click"/&gt;
    &lt;/WrapPanel&gt;
    &lt;Grid Visibility="Visible" d:LayoutOverrides="HorizontalAlignment, VerticalAlignment" HorizontalAlignment="Right" Margin="0,0,16,16" x:Name="propertyPanel" VerticalAlignment="Bottom" Width="169.237" Height="81.455" Grid.ColumnSpan="2" Grid.Row="1"&gt;
     &lt;Rectangle Stroke="#FFFFFFFF" StrokeThickness="3" Margin="0,0,0,0"&gt;
      &lt;Rectangle.BitmapEffect&gt;
       &lt;DropShadowBitmapEffect/&gt;
      &lt;/Rectangle.BitmapEffect&gt;
      &lt;Rectangle.Fill&gt;
       &lt;LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"&gt;
        &lt;GradientStop Color="#FF1E212F" Offset="0"/&gt;
        &lt;GradientStop Color="#FF313551" Offset="1"/&gt;
       &lt;/LinearGradientBrush&gt;
      &lt;/Rectangle.Fill&gt;
     &lt;/Rectangle&gt;
     &lt;Slider HorizontalAlignment="Stretch" Margin="62.633,5,4.681,0" x:Name="exposureSlider" VerticalAlignment="Top" Width="Auto" Height="Auto" Orientation="Horizontal" Maximum="255" ValueChanged="exposureSlider_ValueChanged"/&gt;
     &lt;Label x:Name="serialLabel_Copy" FontSize="10" FontWeight="Bold" Foreground="#FFDEDADA" Content="Exposure" HorizontalAlignment="Left" Margin="3.853,3.853,0,0" VerticalAlignment="Top" Width="57.352"&gt;
      &lt;Label.BitmapEffect&gt;
       &lt;OuterGlowBitmapEffect GlowColor="#FF000000" GlowSize="4" Opacity="0.7"/&gt;
      &lt;/Label.BitmapEffect&gt;
     &lt;/Label&gt;
     &lt;Label x:Name="serialLabel_Copy1" FontSize="10" FontWeight="Bold" Foreground="#FFDEDADA" Content="Threshold" d:LayoutOverrides="Height" HorizontalAlignment="Left" Margin="3.853,27.691,0,0" VerticalAlignment="Top" Width="59.829"&gt;
      &lt;Label.BitmapEffect&gt;
       &lt;OuterGlowBitmapEffect GlowColor="#FF000000" GlowSize="4" Opacity="0.7"/&gt;
      &lt;/Label.BitmapEffect&gt;
     &lt;/Label&gt;
     &lt;Slider x:Name="thresholdSlider" Width="Auto" Orientation="Horizontal" Maximum="253" d:LayoutOverrides="Height, Margin" Margin="62.633,27.691,4.681,0" VerticalAlignment="Top" Minimum="54" Value="54" ValueChanged="thresholdSlider_ValueChanged"/&gt;
     &lt;Button x:Name="greyScaleButton" Width="75.333" Content="Greyscale" Click="greyscaleButton_Click" HorizontalAlignment="Left" Height="21" d:LayoutOverrides="Height" Margin="8,53.761,0,0" VerticalAlignment="Top"/&gt;
     &lt;Button x:Name="Overlay" Height="21" Content="Overlay" Click="OverlayButton_Click" d:LayoutOverrides="Height" HorizontalAlignment="Right" Margin="0,53.761,8,0" VerticalAlignment="Top" Width="64"/&gt;
    &lt;/Grid&gt;
   &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>