문제

I have a Silverlight 5 project that uses Telerik RadGridView. This RadGridView has RowDetails, which contains editable TextBox. If I paste some text in this TextBox several times till it reaches MaxLength, first column in selected grid row is automatically edited with excess text. Has anyone seen and fixed this?

To try out, here`s some 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>

Models

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

Code behind

this.gvMain.ItemsSource = new List<Models.Book>()
  {
     new Book(){ Author = new Author(){ Name = "John", LastName = "Smith"}, 
                 Title = "Dummy", PageCount = 100}
  }; 
도움이 되었습니까?

해결책

It appears that when TextBox or ComboBox in RowDetails cannot paste text from clipboard (when MaxLength is reached for example) they stop precessing pasting events which are relayed to row itself. Ard so row inserts pasted text.

Solution that we implemented is to replace TextBoxes with custom ones that have only this additional code:

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

But ComboBoxes will require some code too. If there`s a better solution please let me know!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top