简体   繁体   English

DataGrid中的DataGridTextColumn CharacterCasing

[英]DataGridTextColumn CharacterCasing in DataGrid

I'm trying to get a DataGridTextColumn to only allow upper casing. 我试图让DataGridTextColumn只允许上壳。

The obvious approach would be to set CharacterCasing to Upper for the EditingElementStyle TextBox . 显而易见的方法是为EditingElementStyle TextBoxCharacterCasing设置为Upper 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). 只要在开始键入之前进入编辑模式,此方法就很好用,但是如果您在单元格不在编辑模式下开始输入时,则在TextBox输入的第一个字符是小写字母(此后,当单元格进入编辑模式时) ,一切都按预期工作)。

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? 我有一种感觉这是一个错误,或者我错过了一些假设将CharacterCasing设置为Upper应该可以解决的问题? 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. 只需将键盘焦点放在DataGrid的第一个单元格中,然后按一下即可进入编辑模式。 Looks like this 看起来像这样

在此输入图像描述

MainWindow.xaml 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 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; }
}

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" . 我没有设置CharacterCasing="Upper" ,而是使用behaviors:TextBoxUpperCaseBehavior.IsEnabled="True"

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

TextBoxUpperCaseBehavior 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();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在WPF DataGrid DataGridTextColumn中编辑成员 - Cannot edit members in WPF DataGrid DataGridTextColumn 如何动态创建DataGrid的DataGridTextColumn并将其绑定? - How to create DataGridTextColumn of DataGrid dynamically and bind it? WPF datagrid在datagridtextcolumn.ElementStyle上的绑定错误 - WPF datagrid binding error on datagridtextcolumn.ElementStyle Silverlight Datagrid如何显式绑定DataGridTextColumn - Silverlight Datagrid how to explicit bind DataGridTextColumn UWP 以编程方式更改 Datagrid 和/或 DataGridTextColumn 绑定? - UWP Changing Datagrid and/or DataGridTextColumn Binding Programatically? 如何将带有值转换器的WPF组合框所选项目绑定到DataGridTextColumn? DataGridTextColumn和combobox都是datagrid列 - How to bind WPF combobox selected item with a Value Converter to a DataGridTextColumn? Both DataGridTextColumn and combobox are datagrid columns 如何在WPF DataGrid的DataGridTextColumn中动态添加多行 - How to add multiple row's dynamically in DataGridTextColumn in wpf datagrid 从DataGridTextColumn获取控件,而无需搜索整个DataGrid子级 - Get a control from DataGridTextColumn without searching through the whole DataGrid children 如何获取WPF DataGrid以使用DataGridTextColumn中的类的成员变量? - How to get WPF DataGrid to use member variables of classes in DataGridTextColumn? 如何在 DataGrid 的 AutoGeneratingColumn 事件中设置 DataGridTextColumn 的 binding.UpdateSourceTrigger - How to set the binding.UpdateSourceTrigger of DataGridTextColumn in AutoGeneratingColumn event of DataGrid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM