Binding DataContext to ValidationRule

前端 未结 7 1921
粉色の甜心
粉色の甜心 2021-02-04 11:33

I have a custom ValidationRule that requires access to the ViewModel in order to validate a supplied value in conjunction with other properties of the ViewModel. I previously tr

7条回答
  •  感情败类
    2021-02-04 12:02

    I use a different approch. Use Freezable objects to make your bindings

      public class BindingProxy : Freezable
        {
                
    
            
                static BindingProxy()
                {
                    var sourceMetadata = new FrameworkPropertyMetadata(
                    delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
                    {
                        if (null != BindingOperations.GetBinding(p, TargetProperty))
                        {
                            (p as BindingProxy).Target = args.NewValue;
                        }
                    });
    
                    sourceMetadata.BindsTwoWayByDefault = false;
                    sourceMetadata.DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    
                    SourceProperty = DependencyProperty.Register(
                        "Source",
                        typeof(object),
                        typeof(BindingProxy),
                        sourceMetadata);
    
                    var targetMetadata = new FrameworkPropertyMetadata(
                        delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
                        {
                            ValueSource source = DependencyPropertyHelper.GetValueSource(p, args.Property);
                            if (source.BaseValueSource != BaseValueSource.Local)
                            {
                                var proxy = p as BindingProxy;
                                object expected = proxy.Source;
                                if (!object.ReferenceEquals(args.NewValue, expected))
                                {
                                    Dispatcher.CurrentDispatcher.BeginInvoke(
                                        DispatcherPriority.DataBind, 
                                        new Action(() =>
                                        {
                                            proxy.Target = proxy.Source;
                                        }));
                                }
                            }
                        });
    
                    targetMetadata.BindsTwoWayByDefault = true;
                    targetMetadata.DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                    TargetProperty = DependencyProperty.Register(
                        "Target",
                        typeof(object),
                        typeof(BindingProxy),
                        targetMetadata);
                }
              
    public static readonly DependencyProperty SourceProperty;   
                public static readonly DependencyProperty TargetProperty;
           
                public object Source
                {
                    get
                    {
                        return this.GetValue(SourceProperty);
                    }
    
                    set
                    {
                        this.SetValue(SourceProperty, value);
                    }
                }
    
               
                public object Target
                {
                    get
                    {
                        return this.GetValue(TargetProperty);
                    }
    
                    set
                    {
                        this.SetValue(TargetProperty, value);
                    }
                }
    
                protected override Freezable CreateInstanceCore()
                {
                    return new BindingProxy();
                }
            }
    
    sHould This have the problem of binding the value too late after the application started. I use Blend Interactions to resolve the problem after the window loads 
    
    

提交回复
热议问题