Binding DataContext to ValidationRule

前端 未结 7 1923
粉色の甜心
粉色の甜心 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 11:59

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

    
      
        
      
      
        
          
        
      
      
        
          
            
          
        
      
    
    

    As for the Binding proxy, here you go: public class BindingProxy : Freezable {

        public static readonly DependencyProperty SourceProperty;
    
        /// 
        /// The target property
        /// 
        public static readonly DependencyProperty TargetProperty;
    
    
        /// 
        /// Initializes static members of the  class.
        /// 
        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);
        }
    
        /// 
        /// Gets or sets the source.
        /// 
        /// 
        /// The source.
        /// 
        public object Source
        {
            get
            {
                return this.GetValue(SourceProperty);
            }
    
            set
            {
                this.SetValue(SourceProperty, value);
            }
        }
    
        /// 
        /// Gets or sets the target.
        /// 
        /// 
        /// The target.
        /// 
        public object Target
        {
            get
            {
                return this.GetValue(TargetProperty);
            }
    
            set
            {
                this.SetValue(TargetProperty, value);
            }
        }
    
        /// 
        /// When implemented in a derived class, creates a new instance of the  derived class.
        /// 
        /// 
        /// The new instance.
        /// 
        protected override Freezable CreateInstanceCore()
        {
            return new BindingProxy();
        }
    }
    

    }

提交回复
热议问题