【问题标题】:Telerik RadGridView: column value is edited when TextBox from RowDetails reaches MaxLengthTelerik RadGridView:当 RowDetails 中的 TextBox 达到 MaxLength 时,编辑列值
【发布时间】:2013-03-14 14:25:33
【问题描述】:

我有一个使用 Telerik RadGridView 的 Silverlight 5 项目。 这个RadGridViewRowDetails,其中包含可编辑的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}
  }; 

【问题讨论】:

    标签: c# telerik silverlight-5.0 radgridview


    【解决方案1】:

    似乎当 RowDetails 中的 TextBoxComboBox 无法从剪贴板粘贴文本时(例如,当到达 MaxLength 时)它们会停止处理中继到行本身的粘贴事件。 Ard 所以行插入粘贴的文本。

    我们实施的解决方案是用只有这个附加代码的自定义文本框替换文本框:

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

    【讨论】:

      猜你喜欢
      • 2016-05-26
      • 1970-01-01
      • 2013-08-18
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多