DataGridTextColumn CharacterCasing in DataGrid

谁说我不能喝 提交于 2020-01-15 12:24:28

问题


I'm trying to get a DataGridTextColumn to only allow upper casing.

The obvious approach would be to set CharacterCasing to Upper for the EditingElementStyle TextBox. This works great as long as you enter edit mode before starting to type but if you start typing when the cell isn't in edit mode then the first character entered in the TextBox is lower case (after this, when the cell has entered edit mode, everything works as expected).

I have a feeling this is a bug or am I missing something with the assumption that setting CharacterCasing to Upper should do the trick? Does anybody have a fix or workaround for this?

The problem can be reproduced like this. Just put the keyboard focus in the first cell in the DataGrid and press a without entering edit mode first. Looks like this

MainWindow.xaml

<DataGrid ItemsSource="{Binding MyList}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Test Character Casing"
                            Binding="{Binding Name}">
            <DataGridTextColumn.EditingElementStyle>
                <Style TargetType="TextBox">
                    <Setter Property="CharacterCasing" Value="Upper"/>
                </Style>
            </DataGridTextColumn.EditingElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MyList = new List<MyItem>();
        MyList.Add(new MyItem { Name = "" });
        MyList.Add(new MyItem { Name = "" });
        this.DataContext = this;
    }
    public List<MyItem> MyList { get; set; }
}

public class MyItem
{
    public string Name { get; set; }
}

回答1:


I'm pretty sure this is a bug and I created a workaround using an Attached Behavior. Instead of setting CharacterCasing="Upper", I use behaviors:TextBoxUpperCaseBehavior.IsEnabled="True".

<Style TargetType="TextBox" x:Key="dataGridUpperCaseTextBoxStyle">
    <Setter Property="behaviors:TextBoxUpperCaseBehavior.IsEnabled" Value="True"/>
</Style>

TextBoxUpperCaseBehavior

public static class TextBoxUpperCaseBehavior
{
    public static readonly DependencyProperty IsEnabledProperty =
        DependencyProperty.RegisterAttached("IsEnabled",
                                            typeof(bool),
                                            typeof(TextBoxUpperCaseBehavior),
                                            new UIPropertyMetadata(false, OnIsEnabledChanged));
    [AttachedPropertyBrowsableForType(typeof(TextBox))]
    public static bool GetIsEnabled(TextBox comboBox)
    {
        return (bool)comboBox.GetValue(IsEnabledProperty);
    }
    public static void SetIsEnabled(TextBox comboBox, bool value)
    {
        comboBox.SetValue(IsEnabledProperty, value);
    }

    private static void OnIsEnabledChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        TextBox textBox = target as TextBox;
        if ((bool)e.NewValue == true)
        {
            textBox.CharacterCasing = CharacterCasing.Upper;
            textBox.TextChanged += textBox_TextChanged;
        }
        else
        {
            textBox.CharacterCasing = CharacterCasing.Normal;
            textBox.TextChanged -= textBox_TextChanged;
        }
    }

    private static void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        if (textBox.Text.ToUpper() != textBox.Text)
        {
            textBox.Text = textBox.Text.ToUpper();
        }
    }
}


来源:https://stackoverflow.com/questions/7890922/datagridtextcolumn-charactercasing-in-datagrid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!