How can I detect when a user is finished editing a property?

Grids for WPF Forum

Posted 8 years ago by Craig - Varigence, Inc.
Version: 11.1.0542
Avatar
In our property grid, we want to have special behaviors run when a user finishes editing certain text properties.

My question is how can I detect when the user has finished editing a property, considering that the properties are data-bound to our view models? It appeared that I could use the PropertyChanged event, but the event seems to fire on each keypress, rather than when I leave the textbox's focus.

Thanks,

-Craig

[Modified at 04/15/2011 02:46 AM]

Comments (5)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

The PropertyChanged event should only fire when the underlying control (i.e. TextBox) pushes a value back to the associated IPropertyDataAccessor.Value property (via a binding). By default, the TextBox property editors should only push the value back when the focus is lost, not on every key stroke.

If you can put together a small sample project that shows what you are trying to do and email it over then we can take a closer look. Be sure to remove any executables or change the extension of the zip file to ensure it gets past our email filters.


Actipro Software Support

Posted 8 years ago by Craig - Varigence, Inc.
Avatar
I was able to determine the problem. For our property grid, we changed the DataTemplate associated with the TextBoxValueTemplateKey. Our DataTemplate is as follows:

<DataTemplate x:Key="{x:Static appropgrid:BuiltinEditors.TextBoxValueTemplateKey}">
    <TextBox 
        x:Name="textBox" 
        Margin="0" 
        Padding="0"
        HorizontalAlignment="Stretch" 
        VerticalAlignment="Stretch"
        BorderThickness="0" 
        Background="Transparent" 
        MaxLines="1"
        Text="{Binding ValueAsString, RelativeSource={RelativeSource AncestorType={x:Type appropgrid:IPropertyDataAccessor}}, NotifyOnValidationError=True, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource noOpConverter}}" 
        />
    <DataTemplate.Triggers>
    ...
    </DataTemplate.Triggers>
</DataTemplate>
Notice that in the Text binding, we have UpdateSourceTrigger set to PropertyChanged; presumably, your default behavior is to update the source on LostFocus. While I can add a LostFocus event to the TextBox, that won't guarantee that the property actually changed when it fires. Do you know of any other way I could combine capturing a LostFocus event with knowing if the property actually changed?

Thanks,

-Craig
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

If you are not using the LostFocus binding trigger, then like you said you'd have to attached your own LostFocus event handler. You would then have to compare the current value of the TextBox with the value of the associated IPropertyDataAccessor.

If you are just concerned with knowing if the property was changed, you could probably use the GotKeyboardFocus, LostKeyboardFocus, and our PropertyChanged event. If the PropertyChanged event is fired while the TextBox has focus, then you know something changed. So you can set a flag to indicate something changed, which would be used by the LostKeyboardFocus event handler.


Actipro Software Support

Posted 8 years ago by Craig - Varigence, Inc.
Avatar
On your first point, regarding comparing the TextBox's current value with the value of the associated IPropertyDataAccessor, I'm not clear on what I'd be comparing.

Let's say I implemented my LostFocus handler like this:

private static void textBox_LostFocus(object sender, RoutedEventArgs e)
{
    var textBox = sender as TextBox;
    if (textBox != null)
    {
        var propertyDataAccessor = textBox.Tag as IPropertyDataAccessor;
        if (propertyDataAccessor != null)
        {
            System.Console.WriteLine("prop value = " + propertyDataAccessor.ValueAsString);
            System.Console.WriteLine("textbox text = " + textBox.Text);
        }
    }
}
Won't propertyDataAccessor.ValueAsString always be equal to textBox.Text, regardless whether the Text property actually changed?

Thanks,

-Craig
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

Sorry, you're right. You'd have to capture the fact that the property changed while the control had focus. Then use that "flag" in the LostFocus event handler.


Actipro Software Support

The latest build of this product (v2019.1 build 0681) was released 18 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.