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));
        }
示例#2
0
        public MainWindow()
        {
            InitializeComponent();

            var file_name = Properties.Settings.Default.DataFilePath;
            var data      = Data.Enterprise.LoadFromFile(file_name);

            DataContext = data;

            PresentationTraceSources.SetTraceLevel(this, PresentationTraceLevel.High);
        }
示例#3
0
        private void SetBinding()
        {
            var b = new Binding()
            {
                Path   = new PropertyPath(TextBox.TextProperty /* <-- Source Property */),
                Source = _source,
                // Mode = BindingMode.TwoWay, <-- implicitly set by WPF
                // UpdateSourceTrigger=UpdateSourceTrigger.LostFocus <-- implicitly set by WPF from TextProperty from Target
            };

            PresentationTraceSources.SetTraceLevel(b, PresentationTraceLevel.High);
            _target.SetBinding(TextBox.TextProperty, b); /* <-- Target Property */
        }
示例#4
0
        private void BindProcessesToListView()
        {
            ObjectDataProvider provider = new ObjectDataProvider();

            provider.ObjectType = typeof(Process);
            provider.MethodName = "GetProcesses";
            Binding binding = new Binding();

            binding.Source = provider;
            binding.Mode   = BindingMode.OneWay;
            PresentationTraceSources.SetTraceLevel(binding,
                                                   PresentationTraceLevel.High);
            listView1.SetBinding(ListView.ItemsSourceProperty, binding);
        }
示例#5
0
        /// <summary>
        /// See <see cref="AnalyzerBase.Analyze" />
        /// </summary>
        public override void Analyze(TreeItem treeItem, AnalyzerContext analyzerContext)
        {
            PresentationTraceSources.SetTraceLevel(treeItem.Instance, PresentationTraceLevel.High);
            var dependencyObject = treeItem.Instance as DependencyObject;

            if (dependencyObject == null)
            {
                return;
            }

            if (_pendingTreeItems.ContainsKey(treeItem))
            {
                return;
            }

            var backgroundHelper = new DataBindingBackgroundHelper(this, treeItem, analyzerContext, () => _pendingTreeItems.Remove(treeItem));

            _pendingTreeItems.Add(treeItem, backgroundHelper);

            foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(dependencyObject.GetType()))
            {
                var dpd = DependencyPropertyDescriptor.FromProperty(property);
                if (dpd != null)
                {
                    BindingExpressionBase binding = BindingOperations.GetBindingExpressionBase(dependencyObject, dpd.DependencyProperty);
                    if (binding != null)
                    {
                        if (binding.HasError || binding.Status != BindingStatus.Active)
                        {
                            var callback = backgroundHelper.CreateCallback();

                            // Ensure that no pending calls are in the dispatcher queue
                            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (Action) delegate
                            {
                                var stringBuilder = new StringBuilder();
                                var stringWriter  = new StringWriter(stringBuilder);
                                var listener      = new TextWriterTraceListener(stringWriter);
                                PresentationTraceSources.DataBindingSource.Listeners.Add(listener);
                                PresentationTraceSources.SetTraceLevel(treeItem.Instance, PresentationTraceLevel.High);

                                // Remove and add the binding to re-trigger the binding error
                                dependencyObject.ClearValue(dpd.DependencyProperty);
                                BindingOperations.SetBinding(dependencyObject, dpd.DependencyProperty, binding.ParentBindingBase);

                                listener.Flush();
                                stringWriter.Flush();

                                Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (Action) delegate
                                {
                                    string bindingError = stringBuilder.ToString();
                                    if (bindingError.Length > 0)
                                    {
                                        int prefix   = bindingError.IndexOf(':');
                                        bindingError = bindingError.Substring(prefix + 6).Replace("\r", "").Replace("\n", "");

                                        callback.Issue = new Issue("BindingError", string.Format("{0}: {1}", dpd.DisplayName, bindingError),
                                                                   IssueSeverity.Error, IssueCategory.Functionality,
                                                                   treeItem, dpd);
                                    }
                                    PresentationTraceSources.DataBindingSource.Listeners.Remove(listener);
                                    listener.Close();

                                    callback.SetDone();
                                });
                            });
                        }
                    }
                }
            }

            backgroundHelper.ReportIssues();
        }