首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF,创建自定义DataGridTextColumn以防止不必要的字符

WPF,创建自定义DataGridTextColumn以防止不必要的字符
EN

Stack Overflow用户
提问于 2015-06-26 07:37:05
回答 2查看 1.2K关注 0票数 3

我新手到WPF,我想阻止用户输入字符,例如。字符"-",因此我使用以下代码创建了自定义DataGridTextColumn:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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在我第二次按下键时起作用。不是第一次。

有人能给我另一个解决办法吗?

编辑:

代码语言:javascript
复制
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处理。

代码语言:javascript
复制
  private void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        var textBox = (TextBox)sender;

        if (textBox.Text.Contains("-"))
        {
            textBox.TextChanged -= OnTextChanged;
            textBox.Text = "";
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-27 13:17:13

这有点麻烦,但我以前也用过,而且通常都很好用。

与其只使用PreviewTextInput,不如将其与TextChanged结合起来。在第一个事件中,只需将当前文本保存在后台字段中,然后在第二个事件中检查无效字符。如果输入了无效字符,则只需重新设置存储在字段中的前一个文本。

代码语言:javascript
复制
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;
    }
}
票数 3
EN

Stack Overflow用户

发布于 2015-06-26 10:30:12

而不是PrepareCellForEdit,您可以尝试GenerateEditingElement

代码语言:javascript
复制
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, Object dataItem)
{
    var textBox = (TextBox)base.GenerateEditingElement(cell, dataItem);
    textBox.PreviewTextInput += OnPreviewTextInput;
    return textBox;
}

我想,应该在PrepareCellForEdit之前调用它,并且在第一次处理键输入之前调用它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31067612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档