0

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

4

2 回答 2

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();


        } }
于 2013-10-23T08:19:56.590 回答
0

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

对于滚动条,您可以将ScrollViewer.VerticalScrollBarVisibility属性设置为Disabled.

于 2013-02-14T14:14:10.720 回答