简体   繁体   中英

WPF DataGridTextColumn contains backspace char

How can "BS" (alias Backspace, alias \\b) be a value in a string property that is databound to a DataGridTextColumn:

XAML

<toolkit:DataGridTextColumn Header="K" 
                            Width="40" 
                            Binding="{Binding Path=Category, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />

C#

private string category = "";
public string Category
{
    get { return category; }
    set
    {
        category = value;
        OnPropertyChanged("Category");
    }
}

I have found entries in the database then containing [BS] as the value of the category. Guaranteed no other code make changes to the property on the way to the database. Couldn't recreate the situation during debugging, because i don't know how to type [BS] in a TextBox obviously. Only one customer could make it somehow...

Do you have any ideas how this could happen? I was trying to implement a check in the property-setter,... but I'm trying to figure out the real issue here.

I created a small WPF application that somewhat recreates the situation. I could get a control character such as backspace (ASCII 8, Alt+0008 ) by copying it from a text editor, and pasting it into the active datagrid cell. NOTE:Make sure you use the numeric keypad for the ASCII code

MainWindow.xaml:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BackspaceInGrid"
        x:Class="BackspaceInGrid.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Last Update Category Value: "/>
            <TextBlock Name="LastUpdateTextBlock"/>
        </StackPanel>
        <DataGrid x:Name="MyDataGrid" Height="300" Width="500" DataContext="" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="K" Width="40" 
                    Binding="{Binding Category, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>

MainWindow.xaml.cs:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

namespace BackspaceInGrid
{
    public class Widget : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string category;
        public string Category
        {
            get { return category; }
            set
            {
                category = value;
                OnPropertyChanged("Category");
            }
        }

        protected void OnPropertyChanged( string name )
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if ( handler != null )
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

    public partial class MainWindow : Window
    {
        public void CategoryChangedEvent( object sender, PropertyChangedEventArgs e )
        {
            Widget widget = ( Widget ) sender;
            string categoryChars = string.Empty;
            for ( int i = 0; i < widget.Category.Length; i++ )
            {
                char c = widget.Category[i];
                if ( char.IsControl(c) )
                {
                    categoryChars = string.Format( "{0}char {1} = ASCII({2}), ", categoryChars, i, (int) c );
                }
                else
                {
                    categoryChars = string.Format( "{0}char {1} = '{2}', ", categoryChars, i, c );
                }
            }
            LastUpdateTextBlock.Text = categoryChars;
        }

        public MainWindow()
        {
            InitializeComponent();
            ObservableCollection< Widget > widgets = new ObservableCollection<Widget>();
            widgets.Add( new Widget { Category = "ABC" } );

            char [] chars_abc = new char [] { (char) 97, (char) 98, (char) 99 };
            char [] chars_ab_back_c = new char [] { (char) 97, (char) 98, (char) 8, (char) 99 };

            widgets.Add( new Widget { Category = new string(chars_abc) } );
            widgets.Add( new Widget { Category = new string(chars_ab_back_c) } );

            foreach ( Widget widget in widgets )
            {
                widget.PropertyChanged += CategoryChangedEvent;
            }
            MyDataGrid.ItemsSource = widgets;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM