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

lztngnrs  于 10个月前  发布在  其他
关注(0)|答案(1)|浏览(132)

我正在写一个WPF应用程序来做意大利语词汇练习。
当我尝试使用Windows键盘快捷键输入重音字符时,它只显示在文本框中作为基础字符。
例如- CTRL键在同一时间的`,然后我只是给我的i -它不把重音在顶部。
下面是XAML
第一个月
它确实给了我给予重音字符在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
    {
    }
}

t8e9dugd

t8e9dugd1#

所以首先你需要捕捉到 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
然后,为了在TextBox中实际添加重音符号,您可以执行以下操作

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;   
    }
}

相关问题