WPF DataGridTextColumn包含退格字符

时间:2017-07-26 12:49:46

标签: wpf backspace datagridtextcolumn

" BS" (别名Backspace,别名\ b)是字符串属性中的值,该属性可以数据绑定到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");
    }
}

我在数据库中找到了条目,然后包含[BS]作为类别的值。保证在去往数据库的路上没有其他代码对属性进行更改。无法在调试过程中重新创建这种情况,因为我不知道如何在TextBox中输入[BS]。只有一个客户能以某种方式成功......

你有什么想法会发生这种情况吗? 我试图在属性设置器中执行检查,但是我试图在这里找出真正的问题。

1 个答案:

答案 0 :(得分:0)

我创建了一个小的WPF应用程序,它稍微重现了这种情况。 我可以通过从文本编辑器复制并将其粘贴到活动数据网格单元格中来获取控制字符,例如 backspace (ASCII 8, Alt + 0008 )。 注意:确保使用ASCII代码的数字键盘

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;
        }
    }
}