无法在 WPF 文本框中输入重音字符

问题描述 投票:0回答:1

我正在编写一个 WPF 应用程序来进行意大利语词汇练习。

当我尝试使用 Windows 键盘快捷键输入重音字符时,它仅作为基础字符出现在文本框中。

例如 - 与 ` 同时按下 CTRL,然后 i 只给我 i - 它不会将重音放在顶部。

这是 XAML

<TextBox TextWrapping="Wrap" TextAlignment="Center" Text="English" Name ="EnglishWord" Width="auto" FontSize="72"/>

它确实给了我 Word 和写字板中的重音字符,所以我不认为这是 Windows 级别的设置。

完整的XAMAL

<Window x:Class="Example.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:Example"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox HorizontalAlignment="Center" Text="" Margin="191,125,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="72" Width="473" Height="99"/>

    </Grid>
</Window>

背后的代码

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 Example
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

AppCS

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Example
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
    }
}
wpf wpf-controls
1个回答
0
投票

所以首先你需要捕获键盘上敲击的ACCENT GRAVE

你可以这样做

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Oem3 && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        _accentGravePressed = true; 
    }
}

请注意,这个重音符号可以根据您的键盘链接到不同的

Key
枚举。在我的上是
Oem3

然后,为了实际将重音添加到文本框中的字符,您可以执行以下操作

private void txtBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (_accentGravePressed )
    {
        var accented = e.Text;
        accented += "\u0301";
        txtBox.Text += accented.Normalize();
        txtBox.CaretIndex = txtBox.Text.Length;

        _accentGravePressed = false;
        e.Handled = true;   
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.