Home > Software engineering >  In WPF, Need Right Click Button Callback
In WPF, Need Right Click Button Callback

Time:08-04

I have a tool bar that I want normal left click to increase the zoom, and a right click of the same button to decrease the zoom. However, I'm uncertain how to get a button callback for a right click event to call my ZoomOut Function. Note, I don't care about making this work on anything other than a desktop with a mouse.

<Window x:Class="WpfApp5.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:local="clr-namespace:WpfApp5"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Name="Zoom" 
         Content="Big Zoom Butten" 
         Click="Zoom_Left_Click" 
         ClickRight="Zoom_Right_Click"/>
    </Grid>
</Window>


using System.Windows;

namespace WpfApp5
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Zoom_Left_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.MessageBox.Show(
                messageBoxText: "Zoom In",
                caption: "Zoom",
                button: System.Windows.MessageBoxButton.OK,
                icon: System.Windows.MessageBoxImage.Information
            );

        }
        private void Zoom_Right_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.MessageBox.Show(
                messageBoxText: "Zoom Out",
                caption: "Zoom",
                button: System.Windows.MessageBoxButton.OK,
                icon: System.Windows.MessageBoxImage.Information
            );
        }

    }
}

CodePudding user response:

You wouldn't use a callback, instead create a property on your view model to hold the current zoom level, and call your ZoomOut or ZoomIn function from the property setter. Then you have the property implement INotifyPropertyChanged, and just bind the zoom level to whatever WPF property you need it for (or read it from the view model as needed).

CodePudding user response:

private void MouseDown(object sender, MouseButtonEventArgs e) 
{
    if (e.RightButton == MouseButtonState.Pressed && e.ClickCount == 1)
    {
        // ZOOM OUT HERE
    }
}
  • Related