其他分享
首页 > 其他分享> > CodeGo.net> WPF,创建自定义DataGridTextColumn,以防止不必要的字符

CodeGo.net> WPF,创建自定义DataGridTextColumn,以防止不必要的字符

作者:互联网

我是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>

Custom 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 = "";
        }
    }

解决方法:

这是黑客的方式,但是我已经用过几次了,它通常可以正常工作.

与其仅使用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;
    }
}

标签:datagridtextcolumn,wpf,c
来源: https://codeday.me/bug/20191028/1950309.html