Silverlight data-binding - is it possible to temporary suspend
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 hov开发者_如何学JAVAers 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
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?
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();
}
}
精彩评论