Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add brackets to text box value

I want to know if there is option that when I type into a text box or drag to anything (I'm using D&D functionality), the text on it will automatically insert brackets. I don't want to do that on the logic or in the code beyond just in the ui. Is that posible?

For example: if I type AAA, I will see in the text box (AAA).

like image 479
mileyH Avatar asked Feb 15 '14 19:02

mileyH


People also ask

How do you add brackets?

Press F8 to insert ( ) and have cursor placed in between. Press F9 to insert [ ] and have cursor placed in between.


2 Answers

I dont want to do that on the logic or in the code beyond

By your conditions it is not possible. Something has to capture the change event and add the brackets to the text. That something is not possible without logic as found in code behind.

The options are

  1. Subscribe to the textblock's SelectionChange event and add the brackets.
  2. Create a custom control which does #1 internal so the consumer doesn't have to do it. (By a technicality it answer's your question).
  3. Put the textblock control between two labels which have the brackets as their context. Bind their visibility to a Boolean on the VM which reports when the bound data of the textblock has changed. If there is text then they become visible, if there is no text it is hidden. Downside is that this is not caught as the user types or until it fully changed, only when exiting the control.

Here is #3

<Label Content="(" Visibility="{Binding HasText, Converter={StaticResource WindowsVisibilityBooleanConverter}}" />
<TextBox  Text="{Binding TextInput}"
            Height="18"
            HorizontalAlignment="Stretch" />
<Label Content=")" Visibility="{Binding HasText, Converter={StaticResource WindowsVisibilityBooleanConverter}}" />
like image 185
ΩmegaMan Avatar answered Sep 23 '22 00:09

ΩmegaMan


Without any of your own logic code, I suppose this is the closest thing to what you want.

<TextBox x:Name="tbInput" />
<TextBlock Text="{Binding ElementName='tbInput', Path=Text, StringFormat={}({0})}" />

The downside would be that you'll always see the empty brackets () if the TextBox is empty.

like image 45
keyboardP Avatar answered Sep 24 '22 00:09

keyboardP