Telerik RadGridView:当RowDetails中的TextBox达到MaxLength时编辑列值
本文关键字:MaxLength 编辑 达到 TextBox RadGridView RowDetails 中的 Telerik | 更新日期: 2023-09-27 18:03:59
我有一个使用Telerik RadGridView
的Silverlight 5项目。这个RadGridView
包含RowDetails
,其中包含可编辑的TextBox
。如果我在这个TextBox
粘贴一些文本几次,直到它达到MaxLength
,所选网格行的第一列会自动编辑多余的文本。有人看到并修复了这个吗?
试一试,这里有一些sode:
XAML
<telerik:RadGridView Name="gvMain" AutoGenerateColumns="False">
<telerik:RadGridView.ChildTableDefinitions>
<telerik:GridViewTableDefinition />
</telerik:RadGridView.ChildTableDefinitions>
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding Title}" />
<telerik:GridViewDataColumn DataMemberBinding="{Binding PageCount}" />
</telerik:RadGridView.Columns>
<telerik:RadGridView.HierarchyChildTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock>Name</TextBlock>
<TextBox Text="{Binding DataContext.Author.Name, RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel}}"
MaxLength="20" Width="100" />
</StackPanel>
</DataTemplate>
</telerik:RadGridView.HierarchyChildTemplate>
public class Author
{
public string Name { get; set; }
public string LastName { get; set; }
}
public class Book
{
public string Title { get; set; }
public int PageCount { get; set; }
public Author Author { get; set; }
}
代码后面
this.gvMain.ItemsSource = new List<Models.Book>()
{
new Book(){ Author = new Author(){ Name = "John", LastName = "Smith"},
Title = "Dummy", PageCount = 100}
};
看来,当TextBox
或ComboBox
在RowDetails不能粘贴文本从剪贴板(当MaxLength
达到例如)他们停止处理粘贴事件中继到行本身。插入粘贴的文本
protected override void OnKeyDown(KeyEventArgs e)
{
if (IsPastingAndClipboardTextIsTooLarge(e.Key))
{
int textToFit = (MaxLength - Text.Length + SelectionLength);
if (textToFit > 0)
{
var startIndex = SelectionStart;
var textToPaste = Clipboard.GetText().Substring(0, Math.Min(textToFit, Clipboard.GetText().Length));
int caretPosition = startIndex + textToPaste.Length;
if (SelectionLength > 0)
Text = Text.Remove(startIndex, SelectionLength);
Text = Text.Insert(startIndex, textToPaste);
SelectionStart = caretPosition;
}
e.Handled = true;
}
else base.OnKeyDown(e);
}
public bool IsPastingAndClipboardTextIsTooLarge(Key key)
{
return key == Key.V && ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) &&
Clipboard.ContainsText() &&
Text.Length + Clipboard.GetText().Length > MaxLength - SelectionLength;
}
但是ComboBoxes
也需要一些代码。如果有更好的解决方案,请告诉我!