Alias Varghese
Alias Varghese

Reputation: 2172

Changing format xxxxxxxxxx to xxx-xxx-xxxx

I have a textbox in WPF and I need to display text in xxx-xxx-xxxx format.

<TextBox FontSize="30" Text="{Binding MyString,UpdateSourceTrigger=PropertyChanged}" Grid.Row="3" MaxLength="10"></TextBox>

MyString is just property which sets value in TextBox into it for some other logic. Can I do it in XAML itself usng StringFormat?

Upvotes: 1

Views: 3359

Answers (3)

Vignesh Prasad V
Vignesh Prasad V

Reputation: 419

You can try using the MaskedTextBox to specify the format of the input.

Example:

<wpfx:MaskedTextBox Mask="000-000-0000" />

Try this tutorial also.

Upvotes: 2

kevchadders
kevchadders

Reputation: 8335

As an option you could split the text in the code behind (or View Model) and bind each value separately

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="{0}-{1}-{2}">
            <Binding Path="FirstPart" />
            <Binding Path="SecondPart" />
            <Binding Path="ThirdPart" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

Upvotes: 2

ckruczek
ckruczek

Reputation: 2410

Yes you can do this. Try the following:

<TextBlock>
    <TextBlock.Text>
      <MultiBinding  StringFormat="{}{0}-whateverhercomes-andwhateverherecomes">
        <Binding Path="MyString"/>
      </MultiBinding>
    </TextBlock.Text>
  </TextBlock>

Upvotes: 0

Related Questions