DataGrid 中的 DataGridTextColumn 字符大小写

发布于 2024-12-11 13:29:47 字数 1708 浏览 0 评论 0原文

我试图让 DataGridTextColumn 仅允许大写。

显而易见的方法是将 EditingElementStyle TextBoxCharacterCasing 设置为 Upper。只要您在开始输入之前进入编辑模式,此功能就非常有效,但如果您在单元格未处于编辑模式时开始输入,则在 TextBox 中输入的第一个字符将是小写(此后,当单元格进入编辑模式时,一切都会按预期工作)。

我感觉这是一个错误,或者我是否在假设将 CharacterCasing 设置为 的情况下遗漏了一些内容>Upper 应该可以解决问题吗?有人对此有解决办法或解决方法吗?

问题可以这样重现。只需将键盘焦点放在 DataGrid 中的第一个单元格中,然后按 a 即可,无需先进入编辑模式。看起来像这样

在此处输入图像描述

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< /em>

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

enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2024-12-18 13:29:47

我很确定这是一个错误,并且我使用附加行为创建了一个解决方法。我没有设置 CharacterCasing="Upper",而是使用 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();
        }
    }
}

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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文