Tuesday, 17 May 2016

Drop down style in WPF

<SolidColorBrush x:Key="ComboBoxNormalBorderBrush" Color="#071c2c" />
    <SolidColorBrush x:Key="ComboBoxNormalBackgroundBrush" Color="#fff" />
    <SolidColorBrush x:Key="ComboBoxNormalBackgroundBrushArrow" Color="#071c2c" />
    <SolidColorBrush x:Key="ComboBoxDisabledForegroundBrush" Color="#888" />
    <SolidColorBrush x:Key="ComboBoxDisabledBackgroundBrush" Color="#eee" />
    <SolidColorBrush x:Key="ComboBoxDisabledBorderBrush" Color="#888" />

    <ControlTemplate TargetType="ToggleButton" x:Key="ComboBoxToggleButtonTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="26" />
            </Grid.ColumnDefinitions>
            <Border Grid.ColumnSpan="2" Name="Border"
              BorderBrush="{StaticResource ComboBoxNormalBorderBrush}"
              CornerRadius="0" BorderThickness="1, 1, 1, 1"
              Background="{StaticResource ComboBoxNormalBackgroundBrush}" />
            <Border Grid.Column="1" Margin="1, 1, 1, 1" BorderBrush="#444" Name="ButtonBorder"
              CornerRadius="0, 0, 0, 0" BorderThickness="0, 0, 0, 0"
              Background="{StaticResource ComboBoxNormalBackgroundBrushArrow}" />

            <Path Name="Arrow" Grid.Column="1"
            Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
            HorizontalAlignment="Center" Fill="White"
            VerticalAlignment="Center" />
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="UIElement.IsMouseOver" Value="True">
                <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="White"/>
                <Setter Property="Shape.Fill" TargetName="Arrow" Value="#071c2c"/>
             
            </Trigger>
            <Trigger Property="ToggleButton.IsChecked" Value="True">
                <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="#071c2c"/>
                <Setter Property="Shape.Fill" TargetName="Arrow" Value="White"/>
            </Trigger>
            <Trigger Property="UIElement.IsEnabled" Value="False">
                <Setter Property="Panel.Background" TargetName="Border" Value="{StaticResource ComboBoxDisabledBackgroundBrush}"/>
                <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="{StaticResource ComboBoxDisabledBackgroundBrush}"/>
                <Setter Property="Border.BorderBrush" TargetName="ButtonBorder" Value="{StaticResource ComboBoxDisabledBorderBrush}"/>
                <Setter Property="TextElement.Foreground" Value="{StaticResource ComboBoxDisabledForegroundBrush}"/>
                <Setter Property="Shape.Fill" TargetName="Arrow" Value="#999"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style  TargetType="{x:Type ComboBox}">
        <Setter Property="UIElement.SnapsToDevicePixels" Value="True"/>
        <Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
        <Setter Property="TextElement.Foreground" Value="#071c2c"/>
        <Setter Property="FrameworkElement.FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="FontFamily" Value="Arial-BoldMT"></Setter>
        <Setter Property="FontSize" Value="30"></Setter>
        <Setter Property="FontWeight" Value="SemiBold"></Setter>
    
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton Name="ToggleButton" Grid.Column="2"
                ClickMode="Press" Focusable="False"
                IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                Template="{StaticResource ComboBoxToggleButtonTemplate}"/>

                        <ContentPresenter Name="ContentSite" Margin="5, 3, 23, 3" IsHitTestVisible="False"
                              HorizontalAlignment="Left" VerticalAlignment="Center"                             
                              Content="{TemplateBinding ComboBox.SelectionBoxItem}"
                              ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"
                              ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"/>
                       
                        <TextBox Name="PART_EditableTextBox" Margin="3, 3, 23, 3"                    
                     IsReadOnly="{TemplateBinding IsReadOnly}"
                     Visibility="Hidden" Background="Transparent"
                     HorizontalAlignment="Left" VerticalAlignment="Center"
                     Focusable="True" >
                            <TextBox.Template>
                                <ControlTemplate TargetType="TextBox" >
                                    <Border Name="PART_ContentHost" Focusable="False" />
                                </ControlTemplate>
                            </TextBox.Template>
                        </TextBox>
                        <!-- Popup showing items -->
                        <Popup Name="Popup" Placement="Bottom"
                   Focusable="False" AllowsTransparency="True"
                   IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}"
                   PopupAnimation="Slide">
                            <Grid Name="DropDown" SnapsToDevicePixels="True"
                    MinWidth="{TemplateBinding FrameworkElement.ActualWidth}"
                    MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}">
                                <Border Name="DropDownBorder" Background="White" Margin="0, 1, 0, 0"
                        CornerRadius="0" BorderThickness="1,1,1,1"
                        BorderBrush="{StaticResource ComboBoxNormalBorderBrush}"/>
                                <ScrollViewer Margin="4" SnapsToDevicePixels="True">
                                    <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" />
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ItemsControl.HasItems" Value="False">
                            <Setter Property="FrameworkElement.MinHeight" TargetName="DropDownBorder" Value="95"/>
                        </Trigger>
                        <Trigger Property="UIElement.IsEnabled" Value="False">
                            <Setter Property="TextElement.Foreground" Value="{StaticResource ComboBoxDisabledForegroundBrush}"/>
                        </Trigger>
                        <Trigger Property="ItemsControl.IsGrouping" Value="True">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
                        </Trigger>
                        <Trigger Property="ComboBox.IsEditable" Value="True">
                            <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                            <Setter Property="UIElement.Visibility" TargetName="PART_EditableTextBox" Value="Visible"/>
                            <Setter Property="UIElement.Visibility" TargetName="ContentSite" Value="Hidden"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!-- End of Flat ComboBox -->

Drawing Canvas in Winforms

  public partial class ImagePanel : UserControl, IComponent
    {

        Point pt;  // the point in the image that will be viewable in the top left corner (0,0) of the viewable rectangle(panel)
        Pen pen = new Pen(Color.Black);     
        public Bitmap canvas;  // Editable drawing
        Graphics canvas_graphics;  
        public List<Point> points; // Points of currently drawing line
        protected Point scrollPosition;
        protected Point clickPosition;
        private int pan_horizontal;
        private int pan_vertical;
        int viewRectWidth, viewRectHeight; // view window width and height
        public bool zoom_scroll_change;
        public bool pen_change;
        public float zoom_coef = 0.02f;
        float zoom = 1.0f;
        Size canvasSize = new Size(60, 40);
        Bitmap background_image; // canvas background - this will not be edited
        InterpolationMode interMode = InterpolationMode.HighQualityBilinear;

        public float Zoom
        {
            get { return zoom; }
            set
            {
                if (value < 0.001f) value = 0.001f;
                RecordZoomScrollPosition();
                zoom = value;
                ChangePenSizeForZoom();
                displayScrollbar();
                setScrollbarValues();
                SetScrollZoomPosition();
                Invalidate();
            }
        }
      
        public Size CanvasSize
        {
            get { return canvasSize; }
            set
            {
                canvasSize = value;
                displayScrollbar();
                setScrollbarValues();
                Invalidate();
            }
        }

     
        public InterpolationMode InterpolationMode
        {
            get { return interMode; }
            set { interMode = value; }
        }


        public ImagePanel()   // Constructor
        {
            InitializeComponent();

            try
            {
                // Set the value of the double-buffering style bits to true.
                SetStyle(ControlStyles.AllPaintingInWmPaint |
                  ControlStyles.UserPaint | ControlStyles.ResizeRedraw |
                  ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);

                pt = new Point(0, 0);  // top left corner of visible rectangle of image
                points = new List<Point>(); // list of points to draw
                pen = new Pen(Color.Black);  // Setup Pen          
                pen.StartCap = LineCap.Round;
                pen.EndCap = LineCap.Round;
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }
 

        public void SetBackgroundAndCanvasImages(Bitmap background_image_input, Bitmap canvas_image_input) // Set background and canvas image
        {
            SetBackgroundImage(background_image_input); // Set background first
            SetCanvasImage(canvas_image_input);   // Set canvas
            RefreshControlsAndDisplay();  // update controls and display
        }

        private void SetBackgroundImage(Bitmap image)  // Set background image
        {
            background_image = image;
        }

        private void SetCanvasImage(Bitmap image)  // Check if canvas exists, otherwise create blank canvas
        {
            canvas = image;
        }
       
        private void RefreshControlsAndDisplay()  // Refresh combined image and display
        {
            displayScrollbar();
            setScrollbarValues();
            Invalidate();
        }


        private void displayScrollbar()
        {
            try
            {
                viewRectWidth = Width;
                viewRectHeight = Height;
                if (background_image != null) canvasSize = background_image.Size;
                if (viewRectWidth > canvasSize.Width * zoom) // If the zoomed image is wider than view window, show the HScrollBar and adjust the view window
                {
                    hScrollBar1.Visible = false;
                    hScrollBar1.Value = 1;
                    viewRectHeight = Height;
                }
                else
                {
                    hScrollBar1.Visible = true;
                    viewRectHeight = Height - hScrollBar1.Height;
                }

                if (viewRectHeight > canvasSize.Height * zoom)   // If the zoomed image is taller than view window, show the VScrollBar and adjust the view window
                {

                    vScrollBar1.Visible = false;
                    vScrollBar1.Value = 1;
                    viewRectWidth = Width;
                }
                else
                {
                    vScrollBar1.Visible = true;
                    viewRectWidth = Width - vScrollBar1.Width;
                }

                // Set up scrollbars
                hScrollBar1.Location = new Point(0, Height - hScrollBar1.Height);
                hScrollBar1.Width = viewRectWidth;
                vScrollBar1.Location = new Point(Width - vScrollBar1.Width, 0);
                vScrollBar1.Height = viewRectHeight;
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }

        private void setScrollbarValues()
        {
            try
            {
                // Set the Maximum, Minimum, LargeChange and SmallChange properties.
                vScrollBar1.Minimum = 0;
                hScrollBar1.Minimum = 0;

                // If the offset does not make the Maximum less than zero, set its value.
                if ((canvasSize.Width * zoom - viewRectWidth) > 0)
                {
                    hScrollBar1.Maximum = (int)(canvasSize.Width * zoom) - viewRectWidth;
                }
                // If the VScrollBar is visible, adjust the Maximum of the
                // HSCrollBar to account for the width of the VScrollBar. 
                if (vScrollBar1.Visible)
                {
                    hScrollBar1.Maximum += vScrollBar1.Width;
                }
                hScrollBar1.LargeChange = hScrollBar1.Maximum / 10;
                hScrollBar1.SmallChange = hScrollBar1.Maximum / 20;

                // Adjust the Maximum value to make the raw Maximum value
                // attainable by user interaction.
                hScrollBar1.Maximum += hScrollBar1.LargeChange;

                // If the offset does not make the Maximum less than zero, set its value.   
                if ((canvasSize.Height * zoom - viewRectHeight) > 0)
                {
                    vScrollBar1.Maximum = (int)(canvasSize.Height * zoom) - viewRectHeight;
                }

                // If the HScrollBar is visible, adjust the Maximum of the
                // VSCrollBar to account for the width of the HScrollBar.
                if (hScrollBar1.Visible)
                {
                    vScrollBar1.Maximum += hScrollBar1.Height;
                }
                vScrollBar1.LargeChange = vScrollBar1.Maximum / 10;
                vScrollBar1.SmallChange = vScrollBar1.Maximum / 20;

                // Adjust the Maximum value to make the raw Maximum value
                // attainable by user interaction.
                vScrollBar1.Maximum += vScrollBar1.LargeChange;
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }

        public void SetMousePostion(Point position)  // Set mouse position
        {
            clickPosition = position;
        }


        public void Pan(Point position)  // Pan the image
        {
            try
            {
                pan_horizontal = scrollPosition.X + clickPosition.X - position.X;  // Mouse traveled X from click position
                pan_vertical = scrollPosition.Y + clickPosition.Y - position.Y; // Mouse traveled Y from click position          
                if (pan_horizontal < 0) pan_horizontal = 0;  // If we get a negative number then we've reached the begginning of the scroll
                if (pan_vertical < 0) pan_vertical = 0;
                if (pan_horizontal >= hScrollBar1.Maximum - hScrollBar1.LargeChange) pan_horizontal = hScrollBar1.Maximum - hScrollBar1.LargeChange - 1; // If we get a negative number then we've reached the begginning of the scroll
                if (pan_vertical >= vScrollBar1.Maximum - vScrollBar1.LargeChange) pan_vertical = vScrollBar1.Maximum - vScrollBar1.LargeChange - 1;
                if (hScrollBar1.Visible) hScrollBar1.Value = pan_horizontal;  // Set new scrollposition
                if (vScrollBar1.Visible) vScrollBar1.Value = pan_vertical;
                Invalidate();
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }


        public void RecordScrollPostion() // Record previous scroll position
        {
            scrollPosition = new Point() { X = hScrollBar1.Value, Y = vScrollBar1.Value };
        }
       
        float zoom_position_previous; // Record previous zoom and scroll position
        Point scroll_maximum;
        public void RecordZoomScrollPosition()
        {
            try
            {
                RecordScrollPostion();
                zoom_position_previous = zoom;
                scroll_maximum = new Point(hScrollBar1.Maximum, vScrollBar1.Maximum);
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }

        }

        // SetScrollPostion
        float h_coef;
        float v_coef;
        public void SetScrollZoomPosition()
        {
            try
            {
                h_coef = (float)scrollPosition.X / (float)scroll_maximum.X;
                v_coef = (float)scrollPosition.Y / (float)scroll_maximum.Y;

                if (hScrollBar1.Visible == true)
                {
                    if ((int)(hScrollBar1.Maximum * h_coef) < hScrollBar1.Minimum) hScrollBar1.Value = hScrollBar1.Minimum;
                    else
                    {
                        hScrollBar1.Value = (int)(hScrollBar1.Maximum * h_coef);
                    }
                }
                if (vScrollBar1.Visible == true)
                {
                    if ((int)(vScrollBar1.Maximum * v_coef) < vScrollBar1.Minimum) vScrollBar1.Value = vScrollBar1.Minimum;
                    else vScrollBar1.Value = (int)(vScrollBar1.Maximum * v_coef);
                }
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }

        }


        // Refresh control if either the vertical or horizontal scroll changes
        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            Invalidate();
        }

        // Add points to be drawn
        public void AddPoint(Point point)
        {
            points.Add(new Point() { X = (int)((float)point.X / zoom) + pt.X, Y = (int)((float)point.Y / zoom) + pt.Y });         
        }

        // Region/area of control to refresh - reduces lag
        List<Point> region = new List<Point>();
        public Region GetRegionByLine(Point point)
        {
            RectangleF rf = new RectangleF(point.X - 50, point.Y - 50, 100, 100);
            return new Region(rf);
        }

        // Keep the size of new pen marking the same regardless of zoom level
        public void ChangePenSizeForZoom()
        {
            pen.Width = (pen_size / zoom);
        }

        /// Change pen size
        float pen_size = 3;
        public void ChangePenSize(int size)
        {
            pen_size = size;
            ChangePenSizeForZoom();
        }

        // Change pen color
        public void ChangePenColor(Color color)
        {
            SolidBrush b = new SolidBrush(color);
            pen.Brush = b; // = color;
        }

        // Set minimum zoom.  Set this so the image doesn't shrink smaller then the veiwing rectangle
        public int SetMinimumZoom()
        {
        
                int width_zoom=0;
                int height_zoom=0;
                try
                {
                    width_zoom = (int)Math.Floor((float)Width / (float)canvasSize.Width / zoom_coef);
                    height_zoom = (int)Math.Floor((float)Height / (float)canvasSize.Height / zoom_coef);
                }
                catch (Exception ex)
                {
                    var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                    Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
                }

                if (width_zoom > height_zoom) return height_zoom;
                else return width_zoom;
           
        }

        // On Paint
        Rectangle srcRect, distRect;
        Matrix mx; // create an identity matrix
        Graphics g;
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                base.OnPaint(e);

                //draw image
                if (background_image != null)
                {
                    if (pen_change == false)
                    {
                        pt = new Point((int)(hScrollBar1.Value / zoom), (int)(vScrollBar1.Value / zoom));  // Get the point in the image that will be viewable in the top left corner of the viewable rectangle(panel)

                        srcRect = new Rectangle(pt, new Size((int)(viewRectWidth / zoom), (int)(viewRectHeight / zoom))); // view a portion of image

                        distRect = new Rectangle((int)(-srcRect.Width / 2), -srcRect.Height / 2, srcRect.Width, srcRect.Height); // the center of apparent image is on origin

                        mx = new Matrix(); // create an identity matrix
                        mx.Scale(zoom, zoom); // zoom image
                        mx.Translate(viewRectWidth / 2.0f, viewRectHeight / 2.0f, MatrixOrder.Append); // move image to view window center

                    }
                    g = e.Graphics; // Get graphics from control

                    g.InterpolationMode = interMode;
                    g.Transform = mx;
                    g.DrawImage(background_image, distRect, srcRect, GraphicsUnit.Pixel);
                    //canvas_graphics.DrawEllipse(pen, pen_location.X, pen_location.Y, 10, 10); // Hovering pen)
                    if (points.Count > 0)
                    {
                        canvas_graphics = Graphics.FromImage(canvas);
                        canvas_graphics.CompositingMode = CompositingMode.SourceCopy;
                        //canvas_graphics.SmoothingMode = SmoothingMode.HighSpeed;
                        canvas_graphics.DrawLines(pen, points.ToArray());
                        g.DrawImage(canvas, distRect, srcRect, GraphicsUnit.Pixel);
                        //canvas_graphics.Dispose();
                    }
                    else g.DrawImage(canvas, distRect, srcRect, GraphicsUnit.Pixel);
                    //g.Dispose();
                    pen_change = false;
                }
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }

        // Save new lines to bitmap
        public void SaveToBitmap()
        {
            try
            {
                if (points.Count == 0)
                    return;

                using (Graphics g = Graphics.FromImage(canvas))
                {
                    if (points.Count == 1)
                    {
                        //g.DrawLine(pen, points[0], points[0]); // Just draw current line on bitmap}
                    }
                    else
                    {
                        g.DrawLines(pen, points.ToArray()); // Just draw current line on bitmap}
                    }
                }
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            try
            {
                displayScrollbar();
                setScrollbarValues();
                base.OnLoad(e);
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }

        protected override void OnResize(EventArgs e)
        {
            try
            {
                displayScrollbar();
                setScrollbarValues();
                base.OnResize(e);
            }
            catch (Exception ex)
            {
                var methodName = new StackTrace(ex).GetFrame(0).GetMethod().Name;
                Errorlog.Errorlog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error", methodName);
            }
        }

    }

Check Application is running WPF winforms C#

 const string appGuid = "d693gba69d1";  //Unique App Id
            bool result;
            System.Threading.Mutex mutex = new System.Threading.Mutex(true, appGuid, out result);

            if (!result)
            {
                MessageBox.Show("Another instance is already running.  It may be loading.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            GC.KeepAlive(mutex);

Changing the coordinate system of canvas WPF

                    <Canvas.LayoutTransform>
                        <ScaleTransform ScaleX="1" ScaleY="-1" CenterX=".5" CenterY=".5" />

                    </Canvas.LayoutTransform>

Call User control event from parent window WPF c#

//In userControl

public event EventHandler HelpButtonUpdated;

        public Header()
        {
            InitializeComponent();
        }

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //Null check makes sure the main page is attached to the event
            if (this.HelpButtonUpdated != null)
                this.HelpButtonUpdated(new object(), new EventArgs());

        }



Main Window
  header.HelpButtonUpdated += new EventHandler(MyEventHandlerFunction_HelpButtonUpdated);
public void MyEventHandlerFunction_HelpButtonUpdated(object sender, EventArgs e)
        {
            PopupHelp.IsOpen = true;
            Opacity = 0.5;          
        }


Auto Timer setup c# WPF

DispatcherTimer AutoSaveTimer; // Auto saver data to file
        TimeSpan auto_save_increment = new TimeSpan(0, 0, 10);  // time in between saves.
       
           //  AutoSaveTimer setup
            AutoSaveTimer = new System.Windows.Threading.DispatcherTimer();
            AutoSaveTimer.Tick += new EventHandler(AutoSaverTimer_Tick);
            AutoSaveTimer.Interval = auto_save_increment;
            AutoSaveTimer.Start();
        


        // Method to perform autosave work
        private void AutoSaverTimer_Tick(object sender, EventArgs e)
        {
          
        }


Altering the Keyboard functionality WPF


    <Window.Resources>
         <!--UI commands.-->
        <RoutedUICommand x:Key="Commands.ZoomOut" />
        <RoutedUICommand x:Key="Commands.ZoomIn" />
    </Window.Resources>

    <Window.InputBindings>       
        <!--Bind keys to commands.-->       
        <KeyBinding
                                                Key="Minus"
                                                Command="{StaticResource Commands.ZoomOut}"
                                                />
        <KeyBinding
                                                Key="Plus"
                                                Command="{StaticResource Commands.ZoomIn}"
                                                />
        <KeyBinding
                                                Key="Space"
                                                Command="{StaticResource Commands.ZoomIn}"
                                                />
     
    </Window.InputBindings>

    <Window.CommandBindings>
       
        <!--Bind commands to event handlers.-->
       
        <CommandBinding
            Command="{StaticResource Commands.ZoomOut}"
            Executed="ZoomOut_Executed"
            />
        <CommandBinding
            Command="{StaticResource Commands.ZoomIn}"
            Executed="ZoomIn_Executed"
            />

    </Window.CommandBindings>