Can I change the properties of a binding in a DataTrigger, without knowing the binding itself?

后端 未结 5 1310
甜味超标
甜味超标 2021-01-04 13:50

I have a TextBox style that formats a number if the box is unfocused, but leaves the number unformatted whlie it\'s being edited.

This is the style I wa

5条回答
  •  情深已故
    2021-01-04 14:23

    Is there a way I can make this style generic, such as only changing the StringFormat property of the binding in the DataTrigger?

    Inherit Style and new XAML would become this:

     
        
            
        
     
    

    Here's the class...

    public class FlyingStyle : Style
    {
        public FlyingStyle()
            : base(typeof(TextBox))
        { }
    
        string _stringFormat;
        public string StringFormat
        {
            get { return _stringFormat; }
            set
            {
                _stringFormat = value;
                CheckInitialize();
            }
        }
        Binding _binding;
        public Binding Binding
        {
            get { return _binding; }
            set
            {
                _binding = value;
                CheckInitialize();
            }
        }
        void CheckInitialize()
        {
            if (StringFormat == null || Binding == null) { return; }// need both
    
            Setters.Add(CreateSetter(Binding, StringFormat));
    
            var trigger = new Trigger
            {
                Property = UIElement.IsKeyboardFocusWithinProperty,
                Value = true,
            };
            trigger.Setters.Add(CreateSetter(Binding));
            Triggers.Add(trigger);
        }
    
        /// Creates the common .
        static Setter CreateSetter(Binding binding, string stringFormat = null)
        {
            // must create a copy, because same binding ref but diff StringFormats
            var bindingCopy = new Binding
            {
                // these could be copies as well
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                ValidatesOnDataErrors = true,
                Mode = BindingMode.TwoWay,
                Path = binding.Path,
    
                AsyncState = binding.AsyncState,
                BindingGroupName = binding.BindingGroupName,
                BindsDirectlyToSource = binding.BindsDirectlyToSource,
                Converter = binding.Converter,
                ConverterCulture = binding.ConverterCulture,
                ConverterParameter = binding.ConverterParameter,
                ElementName = binding.ElementName,
                FallbackValue = binding.FallbackValue,
                IsAsync = binding.IsAsync,
                NotifyOnSourceUpdated = binding.NotifyOnSourceUpdated,
                NotifyOnTargetUpdated = binding.NotifyOnTargetUpdated,
                NotifyOnValidationError = binding.NotifyOnValidationError,
                //StringFormat = set below...
                TargetNullValue = binding.TargetNullValue,
                UpdateSourceExceptionFilter = binding.UpdateSourceExceptionFilter,
                ValidatesOnExceptions = binding.ValidatesOnExceptions,
                XPath = binding.XPath,
                //ValidationRules = binding.ValidationRules
            };
            // mutex ElementName, so modify if needed
            // Source = binding.Source,
            // RelativeSource = binding.RelativeSource,
    
            if (stringFormat != null)
            {
                bindingCopy.StringFormat = stringFormat;
            }
            return new Setter(TextBox.TextProperty, bindingCopy);
        }
    }
    

    Note that my test was

    • the generic MainWindow
    • impl INotifyPropertyChanged
    • SomeValue INPC property
    • DataContext = this
    • x:Name = This

提交回复
热议问题