I am trying to measure an object immediately after changing the DataContext, but the binding for the object is not getting updated soon enough. Here's my code:
// In MeasureOverride(Size)
m_inputWidth = 0.0;
Size elemSize = new Size(double.PositiveInfinity, RowHeight);
MapElementView ruler = new MapElementView();
// Measure inputs
foreach (MapElementViewModel elem in m_vm.InputElements)
{
ruler.DataContext = elem;
ruler.Measure(elemSize);
m_inputWidth = Math.Max(m_inputWidth, ruler.DesiredSize.Width);
}
I want the bindings for the View object to update so that I can measure how large the View needs to be to display the ViewModel. I am reusing the same View to measure because I am virtualizing the data.
Does anyone know how to force the binding to update when the DataContext changes?
Note that the binding does update eventually.
The View contains a TextBlock that is the main element that changes size based on the ViewModel. I have looked at the BindingExpression for the TextProperty on this element immediately after changing the DataContext, but calling UpdateTarget() does not fix the problem and BindingExpression.DataItem appears to be null.
EDIT: The status of the BindingExression is Unattached. The trick is to figure out how to attach it.