5

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).

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
mileyH
  • 176
  • 1
  • 5
  • 20

3 Answers3

3

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}}" />
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
3

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.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • I actually think the brackets will not be empty or at least in my case it shows (0) when the binded property is empty. Here, I am putting the total count of a list in brackets in a datagrid column header - which is exactly what I want. This explains the reason to my point awarded. Thanks for sharing your idea ;-) – omostan May 17 '23 at 07:38
0

See: String format using MultiBinding?

<StackPanel>
  <Slider x:Name="sl1" Minimum="10" Maximum="100"/>
  <Slider x:Name="sl2" Minimum="10" Maximum="100"/>
  <Label x:Name="label13" Background="Yellow" Foreground="Black">
    <Label.Content>
        <TextBlock>
          <TextBlock.Text>
            <MultiBinding StringFormat="{}{0} x {1} Test">
              <Binding ElementName="sl1" Path="Value" />
              <Binding ElementName="sl2" Path="Value" />
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>
    </Label.Content>
  </Label>
</StackPanel>
Community
  • 1
  • 1
MushyPeas
  • 2,469
  • 2
  • 31
  • 48