In the wpf TextBox text box, can characters in a line exceeding a certain length be red?

I use a control TextBoxand want the characters to turn red after a certain number of characters to show the users they typed too much. I don’t want to truncate, because the user could type this “really important thought”, and if I sat down, they would lose it. I have a validation on my base business model that tells me when an entry is invalid and I create my own TextBoxto show the invalidity.

I want a Styletext. Can I do this with help TextBox, or do I need to go to RichTextBox? My base value is just a straight line.

+3
source share
2 answers

I assume that you are using the WPF validation mechanism for stocks ValidationRules. If so, you should determine Triggeron Validation.HasError == trueand, if necessary, set the properties TextBox. For example, the following will highlight text in red if it is invalid.

<TextBox>
  <TextBox.Text>
    <Binding ...>
      <Binding.ValidationRules>
        ...
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
  <TextBox.Style>
    <Style TargetType="{x:Type TextBox}">
      <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
          <Setter Property="Foreground" Value="Red" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </TextBox.Style>
</TextBox>
0
source

This is kind of a crazy answer, and I haven't tried it yet, but if it works, you can continue to use the text field instead of the rich text field.

What to do if you use a gradient brush to draw text (or, if not text, the background of the text field, if that was acceptable to you).

, , . , , , .

, ( , ) , .

+1

Source: https://habr.com/ru/post/1713922/


All Articles