【问题标题】:DataGridTextColumn CharacterCasing in DataGridDataGrid 中的 DataGridTextColumn CharacterCasing
【发布时间】:2011-10-25 14:32:43
【问题描述】:

我正在尝试获取 DataGridTextColumn 以仅允许大写。

显而易见的方法是将CharacterCasing 设置为Upper 用于EditingElementStyle TextBox。只要您在开始输入之前进入编辑模式,这就会很好,但是如果您在单元格不处于编辑模式时开始输入,那么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

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

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 2019-07-27
      • 2013-06-09
      • 1970-01-01
      相关资源
      最近更新 更多