wpf 在c#上获取文本框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23144691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the value of the textbox on c#
提问by M_M
I'm working on a wpf app and i want to get the value of textbox i want to use KeyDown & KeyPress to check if the text is a numeric value but when i write KeyPress the compilator underlined the proprity so i can't use it .
我正在开发一个 wpf 应用程序,我想获取文本框的值,我想使用 KeyDown 和 KeyPress 检查文本是否为数值,但是当我编写 KeyPress 时,编译器在属性下划线,所以我不能使用它.
private void sb_KeyDown_1(object sender, System.Windows.Input.KeyEventArgs e)
{
nonNumberEntered = false;
// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// Determine whether the keystroke is a backspace.
if (e.KeyCode != Keys.Back)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = true;
}
}
}
//If shift key was pressed, it's not a number.
if (Control.ModifierKeys == Keys.Shift)
{
nonNumberEntered = true;
}
}
and it underlined also e.KeyCode and e.KeyNumPad0 .... what should i do ?
它也强调了 e.KeyCode 和 e.KeyNumPad0 .... 我该怎么办?
回答by BradleyDotNET
This isn't the right way to handle this in WPF.
这不是在 WPF 中处理此问题的正确方法。
Getting the value is simple enough, you just bind to something on your View Model:
获取值很简单,你只需绑定到你的视图模型上的东西:
<TextBox Text="{Binding Path=MyTextValue}"/>
To get it to update on every character change, set the UpdateSourceTrigger:
要让它在每次字符更改时更新,请设置 UpdateSourceTrigger:
<TextBox Text="{Binding Path=MyTextValue, UpdateSourceTrigger=OnPropertyChanged}"/>
Since it looks like you are doing validation, I would suggest looking at the MSDN article on Validation in WPF: Binding Validation
由于看起来您正在进行验证,我建议您查看有关 WPF 中的验证的 MSDN 文章:绑定验证
You should (almost) never have to capture actual key-strokes/presses in WPF unless you are writing a game or something similar.
您应该(几乎)永远不必在 WPF 中捕获实际的击键/按下,除非您正在编写游戏或类似的东西。
Here is a question on StackOverflow that could also help: WPF TextBox Validation C#
这是 StackOverflow 上的一个问题,也可以提供帮助:WPF TextBox Validation C#
Since you clearly aren't set up for MVVM yet, here is some code you will need:
由于您显然还没有为 MVVM 设置好,这里有一些您需要的代码:
public class MyViewModel : INotifyPropertyChanged
{
//Standard INotifyPropertyChanged implementation, pick your favorite
private String myTextValue;
public String MyTextValue
{
get { return myTextValue; }
set
{
myTextValue = vaule;
OnPropertyChanged("MyTextValue");
}
}
Then in your codebehind:
然后在你的代码隐藏中:
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
DataContext = new MyViewModel();
}
}
That should be more than enough to get you started (along with the XAML). Let me know if you have any questions!
这应该足以让您入门(以及 XAML)。如果您有任何问题,请告诉我!
回答by Dean Kuga
Bind the Textproperty of your TextBoxin a TwoWaymode and with UpdateSourceTriggerset to PropertyChangedto a public stringproperty supporting change notification in the DataContext(typically a ViewModel) of your view (UserControlor Windowcontaining your TextBox).
在模式中绑定Text您的属性并设置为在您的视图(或包含您的)的(通常为 a )中支持更改通知的属性。TextBoxTwoWayUpdateSourceTriggerPropertyChangedpublic stringDataContextViewModelUserControlWindowTextBox
Once you do that you'll be able to call a method in your ViewModelevery time the TextBoxtext is changed (every time a key is pressed) and do your TextBoxvalue validation there.
一旦你这样做了,你就可以在ViewModel每次TextBox更改文本时(每次按下一个键)调用一个方法并在TextBox那里进行值验证。

