Home > front end >  WPF How do I stop MaskedTextBox from clearing text when I click on it
WPF How do I stop MaskedTextBox from clearing text when I click on it

Time:01-05

This is the xaml of my MaskedTextBox(from Extended WPF Toolkit) which I bind the text with property Time in my datacontext.

<wpfx:MaskedTextBox
    x:Name="MyMaskedTextBox"
    AllowPromptAsInput="True"
    Mask="00:00:00"
    PromptChar="0"
    ResetOnPrompt="False"
    Text="{Binding Time, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

This is the part of my Datacontext that updates it, and my Time property.

public class MyViewModel : ObservableObject, INavigationAware {
    public string Time { 
        get; 
        set; //break point 1
    } 
    public static void UpdateTime(string time) {
        Time = time;
        OnPropertyChanged("Time");
    }
}

The problem is whenever I click on the MaskedTextBox, the text reverts back to the prompt character.

For example I would start up the app and the MaskedTextBox would have 00:00:00. After some event, I update the time property to 12:34:56, and it would correctly show on the UI. But when I click on the MaskedTextBox, it changes the text back to 00:00:00.

Break point 1's call stack just shows "external code". I couldn't find an option in the document to turn this behavior off, how do I stop it?

CodePudding user response:

Try to bind the Value property to your Time property instead of binding the Text property:

<wpfx:MaskedTextBox
    ...
    Value="{Binding Time, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

The control itself sets the Text property based on the Value and the Mask.

  •  Tags:  
  • Related