我需要做类似的东西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 ..
}
它的工作原理,但我想这样做上面的链接
您可以通过创建继承的TextBox新的自定义控制做,在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;
}
}
}
文章来源: Custom TextBox Control that Switches Keyboard Language Automatically c# WPF