【问题标题】:Handle Paste command on DataGridTextColumn处理 DataGridTextColumn 上的粘贴命令
【发布时间】:2013-05-09 05:49:22
【问题描述】:

我有一个要限制输入的 DataGridTextViewColumn。我已经为 PreviewTextInput 和 PreviewKeyDown 事件附加了处理程序,但我还需要通过粘贴命令限制输入。如何处理此文本列的粘贴命令?我的尝试如下:

<DataGridTextColumn Binding="{Binding MyProperty, UpdateSourceTrigger=PropertyChanged}"
                    Width="Auto">
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="TextBox">
            <EventSetter Event="PreviewTextInput"
                         Handler="MyProperty_PreviewTextInput"/>
            <!-- Space, backspace, and delete are not available in PreviewTextInput,
                 so we have to capture them in the PreviewKeyDown event -->
            <EventSetter Event="PreviewKeyDown"
                         Handler="MyProperty_PreviewKeyDown"/>
            <!-- I get a compiler error that "The Property Setter 'CommandBindings'
                 cannot be set because it does not have an accessible set accessor" -->
            <Setter Property="CommandBindings">
                <Setter.Value>
                    <CommandBinding Command="Paste"
                                    Executed="MyProperty_PasteExecuted"/>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTextColumn.EditingElementStyle>                    
</DataGridTextColumn>

我明白为什么我的尝试不起作用,我只是找不到我满意的解决方案来满足我的需求。到目前为止,我发现的唯一解决方案是 2010 年的这篇 SO 帖子 (DataGridTextColumn event binding)。我希望现在有更好的解决方案。

【问题讨论】:

  • 您想限制用户可以输入的字符数,或者您在事件处理程序中进行一些特殊处理?
  • 我确实想限制字符数,但我只是使用 MaxLength 属性来做到这一点。我的问题更具体地说是关于限制允许使用的 what 字符(例如,仅限于字母数字)。
  • 我建议您创建一个attached property 并将其用于遮罩,以便您可以将其用于其他文本框。一些有用的链接 - stackoverflow.com/questions/1103765/…codeproject.com/Articles/34228/…
  • 感谢您提供的链接,已经找到它们 :) 从长远来看,我可能会制定一个可重复使用的解决方案,但对于我现在正在从事的项目,我们只需要一个领域限制输入,因此不值得花时间制定通用解决方案(目前)。
  • 查看发布的答案是否解决了您的问题?

标签: wpf xaml datagrid commandbinding


【解决方案1】:

从错误CommandBindings 属性doesn't have setter 中可以看出,因此您不能从样式中设置它,但如果您将CommandBindings 声明为inline element in TextBox,则可以这样做。

在内部,如果您在 textBox 中将其声明为内联,xaml 会在属性上调用 CommandBindings.Add()。因此,作为一种解决方法,您可以使用DataGridTemplateColumn 并提供CellTemplateCellEditingTemplate 以使其看起来像DataGridTextColumn。它的小样本 -

      <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding MyProperty}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding MyProperty}"
                                 PreviewTextInput="MyProperty_PreviewTextInput"
                                 PreviewKeyDown="MyProperty_PreviewKeyDown">
                            <TextBox.CommandBindings>
                                <CommandBinding Command="Paste" 
                                    Executed="CommandBinding_Executed"/>
                            </TextBox.CommandBindings>
                        </TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>

【讨论】:

  • 啊,对了,我忘记使用 DataTemplate 了,谢谢。我认为使用内联声明调用 CommandBindings.Add() 就是这种情况。
  • 在您最近的编辑(添加了我的 EventSetters)的情况下,您不能直接在 DataTemplate 中的 TextBox 上设置事件,而不是使用样式吗?代码会少一点。
  • 是的。我只是简单地从您的代码中复制/粘贴。会更新的。!!
猜你喜欢
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 2015-01-13
  • 2014-12-02
相关资源
最近更新 更多