我新手到WPF,我想阻止用户输入字符,例如。字符"-",因此我使用以下代码创建了自定义DataGridTextColumn:
public class DataGridNumericColumn : DataGridTextColumn
{
protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
{
var textBox = (TextBox) editingElement;
textBox.PreviewTextInput += OnPreviewTextInput;
return base.PrepareCellForEdit(editingElement, editingEventArgs);
}
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = (TextBox)sender;
if (e.Text == "-")
return;
if (!this.IsNumeric(e.Text))
e.Handled = true;
}
}和XAML:
<ZF:ZFDataGrid
Grid.Row="4" Grid.Column="0"
HorizontalAlignment="Stretch" VerticalAlignment="Top"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
CanUserAddRows="True"
CanUserDeleteRows="False"
CanUserResizeRows="False"
CanUserReorderColumns="False"
CanUserSortColumns="False"
IsSynchronizedWithCurrentItem="True"
SelectionUnit="Cell"
SelectionMode="Single"
Margin="3,3,3,0"
AutoGenerateColumns="False"
AlternatingRowBackground="WhiteSmoke"
RowHeaderWidth="30"
FontSize="18"
ItemsSource="{Binding POSModel}">
<ZF:DataGridNumericColumn Header="Qty" Width="80" />
</ZF:ZFDataGrid>自定义DataGridNumericColumn工作良好,除非我第一次按下字符。如果我按F2编辑或双击列,然后按下键,一切都正常。
但是,如果我不首先编辑单元格而按下键,则自定义DataGridNumericColumn无法工作。
我在PrepareCellForEdit上设置了断点,编码就可以工作了。但是方法OnPreviewTextInput在我第二次按下键时起作用。不是第一次。
有人能给我另一个解决办法吗?
编辑:
protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
{
var textBox = (TextBox) editingElement;
textBox.PreviewTextInput += OnPreviewTextInput;
textBox.TextChanged += OnTextChanged; //change here
return base.PrepareCellForEdit(editingElement, editingEventArgs);
}此代码只运行一次,其余的将由OnPreviewTextInput处理。
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var textBox = (TextBox)sender;
if (textBox.Text.Contains("-"))
{
textBox.TextChanged -= OnTextChanged;
textBox.Text = "";
}
}发布于 2015-06-27 13:17:13
这有点麻烦,但我以前也用过,而且通常都很好用。
与其只使用PreviewTextInput,不如将其与TextChanged结合起来。在第一个事件中,只需将当前文本保存在后台字段中,然后在第二个事件中检查无效字符。如果输入了无效字符,则只需重新设置存储在字段中的前一个文本。
string oldText = string.Empty;
int oldcaret = 0;
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, Object dataItem)
{
var textBox = (TextBox)base.GenerateEditingElement(cell, dataItem);
textBox.PreviewTextInput += OnPreviewTextInput;
textBox.TextChanged += OnTextChanged;
return textBox;
}
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = (TextBox)sender;
oldText = textBox.Text;
oldCaret = textBox.CaretIndex;
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var textBox = (TextBox)sender;
if (textBox.Text.Contains("-"))
{
textBox.Text = oldText;
textBox.CaretIndex = oldCaret;
}
}发布于 2015-06-26 10:30:12
而不是PrepareCellForEdit,您可以尝试GenerateEditingElement
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, Object dataItem)
{
var textBox = (TextBox)base.GenerateEditingElement(cell, dataItem);
textBox.PreviewTextInput += OnPreviewTextInput;
return textBox;
}我想,应该在PrepareCellForEdit之前调用它,并且在第一次处理键输入之前调用它。
https://stackoverflow.com/questions/31067612
复制相似问题