public void SettingTheAttachedPropertyToTheNameOfABoundPropertyOnANonInitializedElementDoesNotAddValidatorRuleToTheReferencedBinding()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            var binding = new Binding("ValidatedStringProperty")
            {
                Mode = BindingMode.OneWayToSource,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding);

            Validate.SetBindingForProperty(textBox, "Text");

            Assert.AreEqual(0, binding.ValidationRules.OfType<ValidatorRule>().Count());
        }
        public void ClearingTheAttachedPropertyRemovesTheValidationRule()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            var binding = new Binding("ValidatedStringProperty")
            {
                Mode = BindingMode.OneWayToSource,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding);
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "Text");

            Assert.AreEqual(1, binding.ValidationRules.OfType<ValidatorRule>().Count());

            textBox.ClearValue(Validate.BindingForPropertyProperty);

            Assert.AreEqual(0, binding.ValidationRules.OfType<ValidatorRule>().Count());
        }
Пример #3
0
        public override FrameworkElement GetElement(string fileName)
        {
            var maxWidth = SystemParameters.WorkArea.Width - 100;
            var maxHeight = SystemParameters.WorkArea.Height - 100;

            var contents = File.ReadAllBytes(fileName);
            var encoding = DetectEncoding(contents);

            var textBox = new TextBox();

            textBox.BeginInit();
            textBox.Width = maxWidth / 2;
            textBox.Height = maxHeight / 2;
            textBox.Text = encoding.GetString(contents);
            textBox.IsReadOnly = true;
            textBox.IsReadOnlyCaretVisible = false;
            textBox.FontFamily = new FontFamily("Consolas");
            textBox.FontSize = 13;
            textBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            textBox.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            textBox.EndInit();

            return textBox;
        }
        public void AttachingValidationToPropertyWithPriorityBindingAddsRulesToTheLeafLevelBindings()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            var binding1 = new Binding("ValidatedStringProperty")
            {
                Mode = BindingMode.OneWayToSource,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            var binding2 = new Binding("ValidatedStringProperty")
            {
                Mode = BindingMode.OneWayToSource,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            BindingOperations.SetBinding(
                textBox,
                TextBox.TextProperty,
                new PriorityBinding
                {
                    Bindings = { binding1, binding2 }
                });
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "Text");

            Assert.AreEqual(1, binding1.ValidationRules.OfType<ValidatorRule>().Count());
            Assert.AreEqual(1, binding2.ValidationRules.OfType<ValidatorRule>().Count());
        }
        public void SettingTheAttachedPropertyTheNameOfAnDependencyPropertyWithAComplexPathBindingThrows()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, new Binding("Complex.Path"));
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "Text");
        }
        public void SettingTheAttachedPropertyTheNameOfAnUnboundDependencyPropertyThrows()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "Text");
        }
        public void SettingTheAttachedPropertyToAValueThatIsNotADependencyPropertyNameThrows()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "InvalidPropertyName");
        }
        public void CanSetARulesetNameAfterSettingTheBindingForPropertyProperty()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            var binding = new Binding("ValidatedStringProperty")
            {
                Mode = BindingMode.OneWayToSource,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding);
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "Text");
            Validate.SetUsingRulesetName(textBox, "Ruleset");

            var rule = binding.ValidationRules.OfType<ValidatorRule>().FirstOrDefault();

            Assert.AreEqual("Ruleset", rule.RulesetName);
        }
        public void CanSetAValidationSpecificationSourceBeforeSettingTheBindingForPropertyProperty()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            var binding = new Binding("ValidatedStringProperty")
            {
                Mode = BindingMode.OneWayToSource,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding);
            textBox.EndInit();

            Validate.SetUsingSource(textBox, ValidationSpecificationSource.Attributes);
            Validate.SetBindingForProperty(textBox, "Text");

            var rule = binding.ValidationRules.OfType<ValidatorRule>().FirstOrDefault();

            Assert.AreEqual(ValidationSpecificationSource.Attributes, rule.ValidationSpecificationSource);
        }
        public void BindingForPropertyPropertyNamedInAttachedProperyIsValidatedForValidValue()
        {
            var instance = new ValidatedObject();

            var textBox = new TextBox();
            textBox.BeginInit();
            textBox.DataContext = instance;
            var binding = new Binding("ValidatedStringProperty")
            {
                Mode = BindingMode.OneWayToSource,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                ValidatesOnExceptions = true
            };
            PresentationTraceSources.SetTraceLevel(binding, PresentationTraceLevel.High);
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding);
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "Text");

            Assert.IsFalse(SWC.Validation.GetHasError(textBox));

            textBox.Text = "aaaaaaaa";

            Assert.IsFalse(SWC.Validation.GetHasError(textBox));
        }
        public void ChangingTheAttachedPropertyRemovesTheValidationRule()
        {
            var textBox = new TextBox();

            textBox.BeginInit();
            var binding1 = new Binding("ValidatedIntProperty")
            {
                Mode = BindingMode.OneWayToSource,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            BindingOperations.SetBinding(textBox, TextBox.MaxLinesProperty, binding1);
            var binding2 = new Binding("ValidatedIntProperty")
            {
                Mode = BindingMode.OneWayToSource,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            BindingOperations.SetBinding(textBox, TextBox.MaxLengthProperty, binding2);
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "MaxLines");

            Assert.AreEqual(1, binding1.ValidationRules.OfType<ValidatorRule>().Count());
            Assert.AreEqual(0, binding2.ValidationRules.OfType<ValidatorRule>().Count());

            Validate.SetBindingForProperty(textBox, "MaxLength");

            Assert.AreEqual(0, binding1.ValidationRules.OfType<ValidatorRule>().Count());
            Assert.AreEqual(1, binding2.ValidationRules.OfType<ValidatorRule>().Count());
        }