Is it possible to control the display of the Extended Wpf Toolkit's MaskedTextBox control?

133 Views Asked by At

I've got an Extended WPF Toolkit's MaskedTextBox control on a user control, that's bound to an int. The value of the int cannot exceed two digits but can be just one digit, which is why I'm using a MaskedTextBox. But we're having a problem displaying this control when creating a new record. Currently, when creating a new record, it displays "00". We only want it to display a single zero, like "_0". I've been following the description for the MaskedTextBox from its GitHub page. This is my first attempt:

<tk:MaskedTextBox x:Name="MaxProficienciesMaskedTextBox"
                  Style="{StaticResource LeftAlignMaskedTextBoxStyle}"
                  Value="{Binding MaxProficiencies}"
                  Mask="#0"
                  ValueDataType="{x:Type System:Int32}"
                  IsEnabled="{Binding IsMaxProficienciesEnabled}" />

I thought, by looking at the GitHub page, that it would give me "_0". It doesn't, it displays "00" for a new record. Likewise, I've tried a mask value of "00" and "90". Both displayed "00" for a new record.

Is it possible to display a new record with "_0"? If so, how?

1

There are 1 best solutions below

1
mm8 On BEST ANSWER

You could always handle the TextChanged event:

private void MaskedTextBox_TextChanged_1(object sender, TextChangedEventArgs e)
{
    TextChange textChange = e.Changes.FirstOrDefault();
    TextBox textBox = (TextBox)sender;
    if (textChange != null && textChange.AddedLength == 2 && textChange.RemovedLength == 0 && textBox.Text == "00")
        textBox.Text = "_0";
}

XAML:

<tk:MaskedTextBox Mask="00" Value="0" ValueDataType="{x:Type System:Int32}"
                  TextChanged="MaskedTextBox_TextChanged_1"/>