WinRT RichEditBox限制

时间:2012-11-19 05:22:28

标签: winrt-xaml richtext

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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;

更多代码背后:

  

private void TextElementControlTextChanged(object sender,   RoutedEventArgs e)           {               string str;               TextElementControl.Document.GetText(Windows.UI.Text.TextGetOptions.None,   出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();


        } }