0

I have a textbox containing a decimal value on my interface that I want to clear whenever the user selects it.

However, if the user doesn't make any changes and selects another interface element I need the text to revert to whatever it was previous to the clear.

So far I have the the following style:

<Style x:Key="CustomTextBoxStyle" TargetType="{x:Type TextBox}">
     <Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="Text" Value="{x:Null}" />
            </Trigger>
        </Style.Triggers>
    </Style>

And then the following to use the style:

<TextBox Style="{DynamicResource CustomTextBoxStyle}" 
                     Tag="{Binding myDecimalValue, StringFormat=#.###}" 
                     TabIndex="1" />

However, in this scenario the value reverts back to what it was even when the user enters a new value.

Can anyone tell me the best way to go about achieving this?

Thanks,

RagtimeWilly
  • 5,265
  • 3
  • 25
  • 41
  • Check this answer - http://stackoverflow.com/a/695751/182344 Looks like that it is what you are need – RredCat Jul 03 '12 at 15:50

3 Answers3

0

The solution here is not to hide the text but to store it in a variable for use later. In C# the code would be something like:

string _originalValue;

public OnFocus(){
    _originalValue = TextBox.Text;
    TextBox.Text = "";
}
public LostFocus(){
    if(TextBox.Text == "")
        TextBox.Text = _originalValue;
}
Ryan Amies
  • 4,902
  • 1
  • 21
  • 36
  • I have the value of the textbox bound a value in my ViewModel. How can I bind the value to another variable based on whether the TextBox has focus? – RagtimeWilly Jul 03 '12 at 15:25
  • @RagtimeWilly - You wouldn't change the binding, you'd change the value of the bound property. This would require having your VM execute a method based on events from the view, though. It's also technically possible, but a violation of MVVM principles, to access the VM directly from an event handler in the code behind. – Esoteric Screen Name Jul 03 '12 at 15:37
0

You could set the forground colour to transparent to hide the text if that's appropriate.

If you actually want to delete the text you should do what Ryan Amies is suggesting on the viewmodel which you should be able to get through the datacontext.

Alan Bradbury
  • 128
  • 2
  • 8
0

Thanks for the help, but I was able to achieve what I was looking for and adhere to MVVM principles by using the AttachedProperty described at the following:

https://stackoverflow.com/a/7972361/1466960

This allowed me to bind the IsFocused property to a value in my view model and proceed in a similar fashion to the one described by Ryan Amies.

View Model:

bool isFocused = false;
double original;

public bool IsFocused
{
    get
    {
        return isFocused;
    }
    set
    {
        isFocused = value;

        if (isFocused)
        {
            original = current;
            current = "";
        }
        else
        {
            if (HundredPercentLine == "")
                current = original;
        }

        OnPropertyChanged(new PropertyChangedEventArgs("IsFocused"));
    }
}
Community
  • 1
  • 1
RagtimeWilly
  • 5,265
  • 3
  • 25
  • 41