Akiner Alkan
Akiner Alkan

Reputation: 6918

How To Prevent Binding Refresh

In my application, i bind the my string property to textblock tooltip. The problem is the property that i bind is updating too frequently in runtime. So every time when it is updated tooltip refreshes even property value is same.

Following is the code:

<TextBlock Text="{Binding Key}" Margin="0,1" ToolTip="{Binding stringProperty}"/> 

When stringProperty is updated tooltip refreshes. I want to bind property and i only want tooltip refresh when the updated value is different or maybe some long time after.

Upvotes: 0

Views: 178

Answers (2)

Michelle
Michelle

Reputation: 56

private string _stringProperty;
public string stringProperty
{
    get { return _stringProperty; }
    set
    {
        if (!ReferenceEquals(_stringProperty, value))
        {
            _stringProperty = value;
            OnPropertyChanged("stringProperty");  
        }
    }
}

If you use ReferenceEquals, it will not throw a NullReferenceException if _stringProperty is null.

Upvotes: 0

Gopichandar
Gopichandar

Reputation: 2842

Something like this.

    public string stringProperty
    {
        get { return _stringProperty; }
        set
        {
            if (!_stringProperty.Equals(value))
            {
                _stringProperty = value;
                OnPropertyChanged("stringProperty");  //Notify UI only if there is new value
            }
        }
    }

If you want you can Compare string with Trim and CaseInsensitive

Upvotes: 1

Related Questions