WPF DataGridTextColumn或DataGridTemplateColumn-在行/单元格中输入的文本 [英] WPF DataGridTextColumn or DataGridTemplateColumn - Text Entered In Row/Cell
问题描述
我大约是C#和WPF的1个月新手.在以下方面,我们将不胜感激:
我有一个DataGridTextColumn和DataGridTemplateColumn.
我已经用一些示例数据预填充了表行/列.
当用户在KEY列行中输入一些文本时,我想:
1.确定用户输入的文本-事件drivien.
2.基于(1),用与特定KEY行/列数据有关的其他数据填充行.
我的DataGridTextColumn中包含以下内容:
Hi,
I am about 1month new to C# and WPF. Would appreciate some help with the following:
I have a DataGridTextColumn and DataGridTemplateColumn.
I have prepopulated the table row/column with some sample data.
When a user enters some text in the KEY column row, I would like to:
1. Determine the text that the user has entered - event drivien.
2. Based on (1), populate the row with additional data pertaining to the specific KEY row/column data.
I have the following in my DataGridTextColumn:
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<eventsetter event="TextInput" handler="TEXT_ENTERED" />
</Style>
在我的C#代码中,
In my C# code,
private void TEXT_ENTERED(object sender, RoutedEventArgs e)
{
TextBox t = (TextBox)sender;
string name = t.Text;
Debug.WriteLine(name);
}
当Event ="TextInput"时,TEXT_ENTERED函数不起作用.当我将事件设置为"TextChanged"时,则对于在DataGridTextColumn中输入的每个字符都会得到一个事件.
我要捕获输入的整个文本-通过在"ABC"中输入,然后用回车,鼠标单击其他位置.... etc.
任何帮助将不胜感激.
另一个有趣的事情是,以上内容适用于Visual Studio 2010中的"TextInput",但不适用于2008年.
谢谢,
Manish
The TEXT_ENTERED function does not work when the Event="TextInput". When I set the Event to "TextChanged", then I get an event for each character entered in the DataGridTextColumn.
I want to capture the whole text entered - either by entered in "ABC" followed by a carriage return, a mouse click elsewhere....etc.
Any help would be appreciated.
Another funny thing is that the above works for "TextInput" in Visual Studio 2010, but not in 2008.
Thanks,
Manish
推荐答案
尽管不是最佳解决方案,但实际上找到了解决方案:
我使用KeyDown作为事件,而不是使用TextInput或TextChanged.
在我的TEXT_ENTERED方法中,我使用KeyEventArgs而不是RoutedEventArgs.
最后,我做到了:
Actually found a solution to this, although it is not the best one:
Instead of using TextInput or TextChanged, I used KeyDown as the event.
In my TEXT_ENTERED method, I used KeyEventArgs instead of RoutedEventArgs.
Finally, I did:
if(e.Key == Key.Return)
{
// Get the Text
}
我希望事情会更加简单....;)
完整摘要:
WPF
I wish things were a bit more staightforward....;)
Full snippet:
WPF
<datagridtextcolumn.editingelementstyle>>
<Style TargetType="{x:Type TextBox}">
<eventsetter event="KeyDown" handler="TEXT_ENTERED" />
</Style>
C#
C#
private void TEXT_ENTERED(object sender, RoutedEventArgs e)
{
if(e.Key == Key.Return)
{
TextBox t = (TextBox)sender;
string name = t.Text;
Debug.WriteLine(name);
}
}
这篇关于WPF DataGridTextColumn或DataGridTemplateColumn-在行/单元格中输入的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!