//
// Summary:
// Initializes a new instance of the System.Windows.Input.RoutedUICommand class,
// using the specified descriptive text, declared name, owner type, and input gestures.
//
//
// Parameters:
// text:
// Descriptive text for the command.
//
// name:
// The declared name of the command for serialization.
//
// ownerType:
// The type that is registering the command.
//
// inputGestures:
// A collection of gestures to associate with the command.
//
// Exceptions:
// T:System.ArgumentNullException:
// name is null.
//
// T:System.ArgumentException:
// ownerType is null.
public RoutedUICommand(string text, string name, Type ownerType, InputGestureCollection inputGestures)
: base(name, ownerType, inputGestures)
{
if (text == null)
{
throw new ArgumentNullException("text");
}
_text = text;
}
//
// Summary:
// Initializes a new instance of the System.Windows.Input.KeyGesture class with
// the specified System.Windows.Input.Key and System.Windows.Input.ModifierKeys.
//
//
// Parameters:
// key:
// The key associated with the gesture.
//
// modifiers:
// The modifier keys associated with the gesture.
//
// Exceptions:
// T:System.ComponentModel.InvalidEnumArgumentException:
// modifiers is not a valid System.Windows.Input.ModifierKeys -or- key is not a
// valid System.Windows.Input.Key.
//
// T:System.NotSupportedException:
// key and modifiers do not form a valid System.Windows.Input.KeyGesture.
public KeyGesture(Key key, ModifierKeys modifiers)
: this(key, modifiers, string.Empty, validateGesture: true)
{
}
//xaml
<Window x:Class="WpfApp107.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:WpfApp107"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.CommandBindings>
<CommandBinding Command="Open" Executed="OnOpen"/>
<CommandBinding Command="IncreaseZoom" Executed="OnZoomIn" CanExecute="OnIsImageExist"/>
<CommandBinding Command="DecreaseZoom" Executed="OnZoomOut" CanExecute="OnIsImageExist"/>
<CommandBinding Command="local:Commands.ZoomNormalCommand" Executed="OnZoomNormal" CanExecute="OnIsImageExist"/>
</Window.CommandBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<ToolBar FontSize="20">
<Button Content="Open..." Command="Open" Margin="6"/>
<Button Content="Zoom In" Command="IncreaseZoom" Margin="6"/>
<Button Content="Zoom Out" Command="DecreaseZoom" Margin="6"/>
<Button Command="local:Commands.ZoomNormalCommand" Content="Normal" Margin="6"/>
</ToolBar>
<ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Image Source="{Binding ImagePath}">
<Image.LayoutTransform>
<ScaleTransform ScaleX="{Binding ZoomLevel}"
ScaleY="{Binding ZoomLevel}"/>
</Image.LayoutTransform>
</Image>
</ScrollViewer>
</Grid>
</Window>
//xaml.cs
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp107
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ImageData _image;
public MainWindow()
{
InitializeComponent();
}
private void OnOpen(object sender, ExecutedRoutedEventArgs e)
{
var dlg = new OpenFileDialog
{
Filter = "Image Files|*.jpg;*.png;*.bmp;*.gif"
};
if (dlg.ShowDialog() == true)
{
_image=new ImageData(dlg.FileName);
DataContext = _image;
}
}
private void OnZoomIn(object sender, ExecutedRoutedEventArgs e)
{
_image.ZoomLevel *= 1.2;
}
private void OnZoomOut(object sender, ExecutedRoutedEventArgs e)
{
_image.ZoomLevel /= 1.2;
}
private void OnIsImageExist(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = _image != null;
}
private void OnZoomNormal(object sender, ExecutedRoutedEventArgs e)
{
_image.ZoomLevel = 1.0;
}
}
class ImageData : INotifyPropertyChanged
{
public string ImagePath { get; private set; }
public ImageData(string pathValue)
{
ImagePath=pathValue;
}
double zoomLevel = 1.0;
public double ZoomLevel
{
get
{
return zoomLevel;
}
set
{
zoomLevel = value;
OnPropertyChanged(nameof(ZoomLevel));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler= PropertyChanged;
if(handler!=null)
{
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
static class Commands
{
static readonly RoutedUICommand _zoomNormalCommand =
new RoutedUICommand("Zoom Normal", "Normal", typeof(Commands),
new InputGestureCollection(new[] {new KeyGesture(Key.N,ModifierKeys.Alt)}));
public static RoutedUICommand ZoomNormalCommand
{
get
{
return _zoomNormalCommand;
}
}
}
}
![]()