views:

73

answers:

2

Hi

I have a group of Siverlight elements that are bound to an object. I want to be able to suspend the databind (effectively freeze their current values) for some time (when the mouse hovers hover the containing panel).

What's the best way to do this? There doesn't seem to be an easy way - one thought is to create a copy of the data object, and set the DataContext to that during the suspension - but that would mean making sure I copy all of the data object's state.

Lee

A: 

Not sure what your entire scenario is, but this sounds like a pure UI challenge to me. Why not create a temp copy of your UI using WriteableBitmap?

Koen Zwikstra
Thanks - I don't think this will work, since I need the UI to be interactive.
Lee Atkinson
A: 

You solution will probably depend on whether or not you want to modify the definition of the data object itself. If you can modify the data object, then you can add a flag which controls whether or not updates are allowed to occur. Then, you can set this property in response to the hover event. Furthermore, any property change events will be queued up and fired after updates are turned back on (assuming that's the behaviour you want).

If you can't modify the object, consider creating a wrapper around it to support this instead.

Here is an example of how to delay events:

class DataObject
{
    private bool _canUpdate = true;
    List<string> propertiesChangedDelayed = new List<string>();

    public bool CanUpdate
    {
        get { return _canUpdate; }
        set
        {
            if (value != _canUpdate) {
                _canUpdate = value;
                if (_canUpdate)
                    NotifyPropertyChangedDelayed();
            }
        }
    }

    protected void NotifyPropertyChanged(string property)
    {
        if (CanUpdate) {
            // fire event
        } else {
            propertiesChangedDelayed.Add(property);
        }
    }

    private void NotifyPropertyChangedDelayed()
    {
        foreach (string property in propertiesChangedDelayed)
        {
            NotifyPropertyChanged(property);
        }
        propertiesChangedDelayed.Clear();
    }
}
antonm
ThanksThe reason is that some elements that are bound to my datasource are editable. Since the datasource properties are changing, I don't want those elements to update as the user is editing them.The data source still needs to be able to update some elements, so I cannot but CanUpdate logic within it.
Lee Atkinson