Telerik RadGridView:当RowDetails中的TextBox达到MaxLength时,将编辑列值

时间:2013-03-14 14:25:33

标签: c# telerik silverlight-5.0 radgridview

我有一个使用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}
  }; 

1 个答案:

答案 0 :(得分:0)

当RowDetails中的TextBoxComboBox无法粘贴剪贴板中的文本时(例如,当达到MaxLength时),它们会停止进行中继到行本身的粘贴事件。 Ard so row插入粘贴文本。

我们实现的解决方案是将TextBoxes替换为仅包含此附加代码的自定义TextBox:

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也需要一些代码。 如果有更好的解决方案,请告诉我!