Home > C#, WinRT > What’s new when using TextBox XAML control in Windos 8.1 Store apps

What’s new when using TextBox XAML control in Windos 8.1 Store apps

18/07/2013

The TextBox XAML control in Windows 8.1 has new interesting properties. Take a look to the following example:

<TextBox Header="Name:" 
         PlaceholderText="Insert your first name" 
         Text="{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

The Header property allows to specify a string that is shown above the TextBox. If we want a more complex caption, we can define the HeaderTemplate property:

<TextBox PlaceholderText="Insert your first name" 
         Text="{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="Name:" Foreground="Red" FontStyle="Italic" />
        </DataTemplate>
    </TextBox.HeaderTemplate>
</TextBox>

Actually, the Header and HeaderTemplate properties aren’t restricted to the TextBox only, but they have are available in other XAML controls, like the ComboBox and, as we have already seen, DatePicker and TimePicker.

With the PlaceholderText property, we can set a watermark that is shown in the TextBox when it is empty. In this way, we have no more the need to use the WatermarkTextBox from Callisto or WinRT XAML Toolkit.

Another important addition concerns Binding at all, but it is particularly important for TextBox: with the new UpdateSourceTrigger property, when set to PropertyChanged, the ViewModel property bound to the Text property of the TextBox (UserName in this example) is automatically updated as soon as the user changes the text in the UI. So, we don’t need to use anymore a solution like the one described in the post How to immediately update the source of a TextBox in WinRT with C#.

Categories: C#, WinRT
  1. No comments yet.
  1. 19/07/2013 at 03:15
Comments are closed.
%d bloggers like this: