我正在将FrameworkElement从WPF页面上的StackPanel传输到以编程方式创建的StackPanel。在某些时候,我需要刷新绑定(bind)并获取属性值。

此时,对于所有绑定(bind)表达式,FrameworkElement.DataContext具有正确的值和BindingExpression.Status==BindingStatus.Unattached。我执行BindingExpression.UpdateTarget,但属性值为空。

我找到了BindingExpression.UpdateTarget()的源代码:

    public override void UpdateTarget()
    {
        if (Status == BindingStatus.Detached)
            throw new InvalidOperationException(SR.Get(SRID.BindingExpressionIsDetached));

        if (Worker != null)
        {
            Worker.RefreshValue();  // calls TransferValue
        }
    }

worker 在内部重写void BindingExpression.Activate()中获取实例。同样在Activate()BindingExpression.Status内部设置为BindingStatus.Active。

如何以编程方式启动BindingExpression.Activate()的执行并在此之后执行UpdateTarget?

更新

这里的解决方案(谢谢Olli):
public  void UpdateAllBindingTargets( DependencyObject obj)
    {

        FrameworkElement visualBlock = obj as FrameworkElement;
        if (visualBlock==null)
            return;
        if (visualBlock.DataContext==null)
            return;
        Object objDataContext = visualBlock.DataContext;

        IEnumerable<KeyValuePair<DependencyProperty, BindingExpression>> allElementBinding = GetAllBindings(obj);
        foreach (KeyValuePair<DependencyProperty, BindingExpression> bindingInfo in allElementBinding)
        {
            BindingOperations.ClearBinding(obj, bindingInfo.Key);

            Binding myBinding = new Binding(bindingInfo.Value.ParentBinding.Path.Path);
            myBinding.Source = objDataContext;
            visualBlock.SetBinding(bindingInfo.Key, myBinding);
            BindingOperations.GetBindingExpression(visualBlock, bindingInfo.Key).UpdateTarget();

        }

    }

在哪里获取对象的所有绑定(bind):
public  IEnumerable<KeyValuePair<DependencyProperty, BindingExpression>> GetAllBindings( DependencyObject obj)
    {
        var stack = new Stack<DependencyObject>();

        stack.Push(obj);

        while (stack.Count > 0)
        {
            var cur = stack.Pop();
            var lve = cur.GetLocalValueEnumerator();

            while (lve.MoveNext())
                if (BindingOperations.IsDataBound(cur, lve.Current.Property))
                {
                    KeyValuePair<DependencyProperty,BindingExpression> result=new KeyValuePair<DependencyProperty, BindingExpression>(lve.Current.Property,lve.Current.Value as BindingExpression);
                    yield return result;
                }

            int count = VisualTreeHelper.GetChildrenCount(cur);
            for (int i = 0; i < count; ++i)
            {
                var child = VisualTreeHelper.GetChild(cur, i);
                if (child is FrameworkElement)
                    stack.Push(child);
            }
        }
    }

最佳答案

您可以以编程方式创建一个新的绑定(bind),这可以解决问题。

http://msdn.microsoft.com/en-us/library/ms752347.aspx#creating_a_binding

MSDN页面中的示例

MyData myDataObject = new MyData(DateTime.Now);
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
myText.SetBinding(TextBlock.TextProperty, myBinding);

关于c# - 如何通过编程将BindingExpression.Status设置为Active?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12821107/

10-09 00:09