前端XAML代码:
<Window x:Class="WpfApplication48.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:WpfApplication48"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<RoutedUICommand x:Key="IncreaseFont" />
<RoutedUICommand x:Key="DecreaseFont" />
</Window.Resources>
<Window.InputBindings>
<KeyBinding Gesture="Ctrl+Alt+I" Command="{StaticResource IncreaseFont}" />
<KeyBinding Gesture="Ctrl+Alt+D" Command="{StaticResource DecreaseFont}" />
</Window.InputBindings>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource IncreaseFont}" Executed="OnIncreaseFont" />
<CommandBinding Command="{StaticResource DecreaseFont}" Executed="OnDecreaseFont" />
</Window.CommandBindings>
<Grid>
<Grid.Resources>
<RoutedUICommand x:Key="IncreaseFont1" />
<RoutedUICommand x:Key="DecreaseFont1" />
</Grid.Resources>
<Grid.CommandBindings>
<CommandBinding Command="{StaticResource IncreaseFont1}" Executed="OnIncreaseFont" />
<CommandBinding Command="{StaticResource DecreaseFont1}" Executed="OnDecreaseFont" />
</Grid.CommandBindings>
<TextBox Name="TB1" Text="Daniel Text" Width="300" Height="150" FontSize="15"/>
<Button x:Name="button" Content="增加字体" HorizontalAlignment="Left" Margin="110,266,0,0" VerticalAlignment="Top" Width="75" Command="{StaticResource IncreaseFont1}" />
<Button x:Name="button1" Content="减小字体" HorizontalAlignment="Left" Margin="325,265,0,0" VerticalAlignment="Top" Width="75" Command="{StaticResource DecreaseFont1}"/>
</Grid>
</Window>
后台C#代码:
using System;
using System.Collections.Generic;
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 WpfApplication48
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void OnIncreaseFont(object sender, ExecutedRoutedEventArgs e)
{
TB1.FontSize += 5;
}
public void OnDecreaseFont(object sender, ExecutedRoutedEventArgs e)
{
TB1.FontSize -= 5;
}
}
}
参考链接:
http://kb.cnblogs.com/page/68526/