2012-11-19 35 views
0

在Windows应用商店应用(XAML/C#)中,我创建了一个带有'RichEditBox'的自定义控件。我想限制它的字符数(MaxLength),或者至少应该禁用垂直滚动条。我怎样才能实现它?WinRT RichEditBox局限性

回答

0

我认为不可能以声明方式设置字符数限制,但是您可以处理文本更改的事件并在代码后面执行代码检查。

对于滚轮可以将ScrollViewer.VerticalScrollBarVisibility属性设置为Disabled

0

这里尝试了这一点:

<RichEditBox x:Name="TextElementControl" 
    Background="{Binding Background, ElementName=userControlModified}" 
    ManipulationMode="None" 
    ScrollViewer.HorizontalScrollMode="Disabled" 
    AcceptsReturn="True" TextWrapping="Wrap" 
    SizeChanged="TextElementControlSizeChanged" 
    IsDoubleTapEnabled="False" 
    BorderThickness="0" 
    BorderBrush="{x:Null}" 
    Padding="10,10,10,10"  
/> 

代码背后:

TextElementControl.TextChanged + = TextElementControlTextChanged;

更多的代码背后:

私人无效TextElementControlTextChanged(对象发件人, RoutedEventArgs E) { 字符串str; TextElementControl.Document.GetText(Windows.UI.Text.TextGetOptions.None, out str); TextElementControl.Height = double.NaN;

 if (str.Trim().Length > 502 && !_loading) 
     { 
      if (popUpReminder == null) 
      { 
       popUpReminder = new Popup(); 
       popUpReminder.IsLightDismissEnabled = true; 

       var panel = new StackPanel(); 
       panel.Background = BlackSolidColorBrush; 
       panel.Height = 60; 
       panel.Width = 220; 

       var reminderText = new TextBlock(); 
       reminderText.FontSize = 14; 
       reminderText.Text = "You have exceeded the maximum number of characters for this textbox."; 
       reminderText.TextWrapping = TextWrapping.Wrap; 

       reminderText.Focus(Windows.UI.Xaml.FocusState.Programmatic); 
       reminderText.Margin = new Thickness(10, 5, 10, 5); 
       panel.Children.Add(reminderText); 

       Border brder = new Border(); 
       brder.BorderBrush = RedSolidColorBrush; 
       brder.BorderThickness = new Thickness(2); 
       brder.Child = panel; 

       popUpReminder.Child = brder; 
       popUpReminder.HorizontalOffset = Window.Current.CoreWindow.Bounds.Width - panel.Width - 10; 
       popUpReminder.VerticalOffset = Window.Current.CoreWindow.Bounds.Bottom - 100 - panel.Height - 10; 
      } 
      popUpReminder.IsOpen = true; 
      TextElementControl.Document.Undo(); 


     } }