コード例 #1
0
        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;
        }
コード例 #4
0
        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());
        }
コード例 #5
0
        public void SettingTheAttachedPropertyTheNameOfAnDependencyPropertyWithAComplexPathBindingThrows()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, new Binding("Complex.Path"));
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "Text");
        }
コード例 #6
0
        public void SettingTheAttachedPropertyTheNameOfAnUnboundDependencyPropertyThrows()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "Text");
        }
コード例 #7
0
        public void SettingTheAttachedPropertyToAValueThatIsNotADependencyPropertyNameThrows()
        {
            var textBox = new TextBox();
            textBox.BeginInit();
            textBox.EndInit();

            Validate.SetBindingForProperty(textBox, "InvalidPropertyName");
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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));
        }
コード例 #11
0
        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());
        }