none
WPF 4.0里面怎么才能有Delay属性啊 RRS feed

  • 问题

  • <TextBox Text="{Binding title,Delay=500}"/>

    这个在4.5中有。 4.0中怎样才能拥有呢(为了支持xp,没法升级到4.5)?

    2017年8月29日 6:40

全部回复

  • 你好,

    同样的问题在这里:Delay property on Binding from .Net 4.5 in .Net 4.0

    引用代码:

    [MarkupExtensionReturnType(typeof(object))]
    public class DelayedBindingExtension : MarkupExtension
    {
        private readonly Binding _binding = new Binding();
    
        public DelayedBindingExtension()
        {
            //Default value for delay
            Delay = TimeSpan.FromSeconds(0.5);
        }
    
        public DelayedBindingExtension(PropertyPath path)
            : this()
        {
            Path = path;
        }
    
        #region properties
    
        [DefaultValue(null)]
        public object AsyncState
        {
            get { return _binding.AsyncState; }
            set { _binding.AsyncState = value; }
        }
    
        [DefaultValue(false)]
        public bool BindsDirectlyToSource
        {
            get { return _binding.BindsDirectlyToSource; }
            set { _binding.BindsDirectlyToSource = value; }
        }
    
        [DefaultValue(null)]
        public IValueConverter Converter
        {
            get { return _binding.Converter; }
            set { _binding.Converter = value; }
        }
    
        [TypeConverter(typeof(CultureInfoIetfLanguageTagConverter)), DefaultValue(null)]
        public CultureInfo ConverterCulture
        {
            get { return _binding.ConverterCulture; }
            set { _binding.ConverterCulture = value; }
        }
    
        [DefaultValue(null)]
        public object ConverterParameter
        {
            get { return _binding.ConverterParameter; }
            set { _binding.ConverterParameter = value; }
        }
    
        [DefaultValue(null)]
        public string ElementName
        {
            get { return _binding.ElementName; }
            set { _binding.ElementName = value; }
        }
    
        [DefaultValue(null)]
        public object FallbackValue
        {
            get { return _binding.FallbackValue; }
            set { _binding.FallbackValue = value; }
        }
    
        [DefaultValue(false)]
        public bool IsAsync
        {
            get { return _binding.IsAsync; }
            set { _binding.IsAsync = value; }
        }
    
        [DefaultValue(BindingMode.Default)]
        public BindingMode Mode
        {
            get { return _binding.Mode; }
            set { _binding.Mode = value; }
        }
    
        [DefaultValue(false)]
        public bool NotifyOnSourceUpdated
        {
            get { return _binding.NotifyOnSourceUpdated; }
            set { _binding.NotifyOnSourceUpdated = value; }
        }
    
        [DefaultValue(false)]
        public bool NotifyOnTargetUpdated
        {
            get { return _binding.NotifyOnTargetUpdated; }
            set { _binding.NotifyOnTargetUpdated = value; }
        }
    
        [DefaultValue(false)]
        public bool NotifyOnValidationError
        {
            get { return _binding.NotifyOnValidationError; }
            set { _binding.NotifyOnValidationError = value; }
        }
    
        [DefaultValue(null)]
        public PropertyPath Path
        {
            get { return _binding.Path; }
            set { _binding.Path = value; }
        }
    
        [DefaultValue(null)]
        public RelativeSource RelativeSource
        {
            get { return _binding.RelativeSource; }
            set { _binding.RelativeSource = value; }
        }
    
        [DefaultValue(null)]
        public object Source
        {
            get { return _binding.Source; }
            set { _binding.Source = value; }
        }
    
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public UpdateSourceExceptionFilterCallback UpdateSourceExceptionFilter
        {
            get { return _binding.UpdateSourceExceptionFilter; }
            set { _binding.UpdateSourceExceptionFilter = value; }
        }
    
        [DefaultValue(UpdateSourceTrigger.Default)]
        public UpdateSourceTrigger UpdateSourceTrigger
        {
            get { return _binding.UpdateSourceTrigger; }
            set { _binding.UpdateSourceTrigger = value; }
        }
    
        [DefaultValue(null)]
        public object TargetNullValue
        {
            get { return _binding.TargetNullValue; }
            set { _binding.TargetNullValue = value; }
        }
    
        [DefaultValue(null)]
        public string StringFormat
        {
            get { return _binding.StringFormat; }
            set { _binding.StringFormat = value; }
        }
    
        [DefaultValue(false)]
        public bool ValidatesOnDataErrors
        {
            get { return _binding.ValidatesOnDataErrors; }
            set { _binding.ValidatesOnDataErrors = value; }
        }
    
        [DefaultValue(false)]
        public bool ValidatesOnExceptions
        {
            get { return _binding.ValidatesOnExceptions; }
            set { _binding.ValidatesOnExceptions = value; }
        }
    
        [DefaultValue(null)]
        public string XPath
        {
            get { return _binding.XPath; }
            set { _binding.XPath = value; }
        }
    
        [DefaultValue(null)]
        public Collection<ValidationRule> ValidationRules
        {
            get { return _binding.ValidationRules; }
        }
    
        #endregion
    
        [DefaultValue(null)]
        public TimeSpan Delay { get; set; }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            try
            {
                _binding.Mode = BindingMode.TwoWay;
                _binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
            }
            catch (InvalidOperationException)  //Binding in use already don't change it
            {
            }
    
            var valueProvider = serviceProvider.GetService(typeof (IProvideValueTarget)) as IProvideValueTarget;
            if (valueProvider != null)
            {
                var bindingTarget = valueProvider.TargetObject as DependencyObject;
                var bindingProperty = valueProvider.TargetProperty as DependencyProperty;
                if (bindingProperty != null && bindingTarget != null)
                {
                    var result = (BindingExpression)_binding.ProvideValue(serviceProvider);
    
                    new DelayBindingManager(result, bindingTarget, bindingProperty, Delay);
                    return result;
                }
            }
    
            return this;
        }
    
        private class DelayBindingManager
        {
            private readonly BindingExpressionBase _bindingExpression;
            private readonly DependencyProperty _bindingTargetProperty;
            private DependencyPropertyDescriptor _descriptor;
            private readonly DispatcherTimer _timer;
    
            public DelayBindingManager(BindingExpressionBase bindingExpression, DependencyObject bindingTarget, DependencyProperty bindingTargetProperty, TimeSpan delay)
            {
                _bindingExpression = bindingExpression;
                _bindingTargetProperty = bindingTargetProperty;
    
                _descriptor = DependencyPropertyDescriptor.FromProperty(_bindingTargetProperty, bindingTarget.GetType());
                if (_descriptor != null)
                    _descriptor.AddValueChanged(bindingTarget, BindingTargetTargetPropertyChanged);
    
                _timer = new DispatcherTimer();
                _timer.Tick += TimerTick;
                _timer.Interval = delay;
            }
    
            private void BindingTargetTargetPropertyChanged(object sender, EventArgs e)
            {
                var source = (DependencyObject)sender;
                if (!BindingOperations.IsDataBound(source, _bindingTargetProperty))
                {
                    if (_descriptor != null)
                    {
                        _descriptor.RemoveValueChanged(source, BindingTargetTargetPropertyChanged);
                        _descriptor = null;
                    }
                    return;
                }
    
                _timer.Stop();
                _timer.Start();
            }
    
            private void TimerTick(object sender, EventArgs e)
            {
                _timer.Stop();
                _bindingExpression.UpdateSource();
            }
        }
    }

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年8月29日 8:38
    版主
  • 版主:

            请问你能告诉我怎样用上面的代码么,我一片茫然。我看stackoverflow也没有写怎么用啊。

    2017年8月29日 9:48
  • 你好,

    抱歉,回复晚了。

    关于WPF的自定义扩展标记,建议你参考这篇文章:http://www.cnblogs.com/loveis715/archive/2012/02/06/2340669.html

       <Slider x:Name="slider"
                    Width="239"
                    Margin="23,26,0,0"
                    HorizontalAlignment="Left" 
                    VerticalAlignment="Top"
                    Maximum="100" 
                    Minimum="0"
                    Value="{local:DelayedBinding ElementName=textBox,
                                                 Path=Text,
                                                 Mode=TwoWay,
                                                 UpdateSourceTrigger=PropertyChanged}"/>
            <TextBox x:Name="textBox"
                     Width="138" Height="23"
                     Margin="68,92,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"
                     TextWrapping="Wrap"/>
    
        </Grid>

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年8月31日 5:32
    版主