我需要做那样的事情 http://www.codeproject.com/Articles/301832/Custom-Text-box-Control-that-Switch-keyboard-langu 但对于 WPF 和C#
我试着用一个简单的 if 语句来做,但是我必须放一个像这样的文本框
private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
{
textBox1.Focus();
if (textBox1.Text == "Q" || textBox1.Text == "q")
{
textBox2.Text = textBox2.Text+ "ض";
textBox1.Text = "";
}
else if (textBox1.Text == "W" || textBox1.Text == "w")
{
textBox2.Text = textBox2.Text + "ص";
textBox1.Text = "";
}
// and so ..
}
它有效,但我想做一些像上面的链接
答案 0 :(得分:1)
您可以通过创建继承TextBox的新自定义Control在WPF中执行此操作。在那里创建一个新的TextLanguage属性并覆盖OnKeyDown方法
namespace WpfApplication
{
public enum TextLanguage
{
English,
Arabic
}
public class CustomTextBox : TextBox
{
public TextLanguage TextLanguage { get; set; }
protected override void OnKeyDown(KeyEventArgs e)
{
if (TextLanguage != WpfApplication.TextLanguage.English)
{
e.Handled = true;
if (Keyboard.Modifiers == ModifierKeys.Shift)
{
// Shift key is down
switch (e.Key)
{
case Key.Q:
AddChars("ص");
break;
// Handle Other Cases too
default:
e.Handled = false;
break;
}
}
else if (Keyboard.Modifiers == ModifierKeys.None)
{
switch (e.Key)
{
case Key.Q:
AddChars("ض");
break;
// Handle Other Cases too
default:
e.Handled = false;
break;
}
}
}
base.OnKeyDown(e);
}
void AddChars(string str)
{
if (SelectedText.Length == 0)
AppendText(str);
else
SelectedText = str;
this.SelectionLength = 0;
this.CaretIndex = Text.Length;
}
}
}