【发布时间】: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