对wpf表单的附加验证

时间:2016-07-27 15:36:19

标签: wpf visual-studio xaml

我有一个用于帮助台系统的xaml表单控件,用于检查字段是否已完成。如果是,则允许用户保存表单。还有一个覆盖,因此在模板模式下,用户可以在没有所需验证的情况下进行更改。代码在

之下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

//Additional
using System.ComponentModel;
using System.ComponentModel.Design;

//SCSM
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.UI.DataModel;
using Microsoft.EnterpriseManagement.UI.SdkDataAccess;
using Microsoft.EnterpriseManagement.ConsoleFramework;
using Microsoft.EnterpriseManagement.UI.WpfControls;
using Microsoft.EnterpriseManagement.GenericForm;

//Local
using ChangeFormControl.Validation;

namespace ChangeFormControl
{
    public partial class ValidationControl : UserControl
    {
        public ValidationControl()
        {
            InitializeComponent();
        }

        private void FormControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext is IDataItem)
            {
                //Do not add validation if in template mode
                if (!FormUtilities.Instance.IsFormInTemplateMode(this)) ProcessControls();
            }
        }

        private void ProcessControls()
        {
            try
            {
                //Attempt to get the parent TabControl of our custom control
                //There is a TabControl near the top of the tree, we want the one below that
                DependencyObject doParentRoot = GetParentDependancyObject(this, "System.Windows.Controls.TabControl");

                //Did we get the TabControl?
                if (doParentRoot != null)
                {
                    //Now process each TabItem on the tab control
                    foreach (DependencyObject doChild in LogicalTreeHelper.GetChildren(doParentRoot))
                    {
                        //Is this a TabItem?
                        if (doChild.GetType().ToString() == "System.Windows.Controls.TabItem")
                        {
                            //Process each child on current tab to find ones we want and add validation rules
                            AddValidation(doChild);
                        }
                    }
                }

            }
            catch
            {
            }
        }

        //Navigates the Logical tree for passed parent and adds required validation
        private void AddValidation(DependencyObject rootparent)
        {
            //Navigate the logical tree for the children          
            foreach (var rootChild in LogicalTreeHelper.GetChildren(rootparent))
            {
                try
                {
                    if (rootChild is DependencyObject)
                    {

                        if (rootChild is TextBox && ((TextBox)rootChild).Name == "titleTextBox")
                        {
                            //Add the rule so that this property is now required
                            TextBox tb = (TextBox)rootChild;
                            TextRule rule = new TextRule();
                            BindingOperations.GetBinding(tb, TextBox.TextProperty).ValidationRules.Add(rule);
                        }
                        
                    }
                }
                catch
                {
                }

                //Process further logical children of this child
                if (rootChild is DependencyObject) AddValidation(rootChild as DependencyObject);
            }
        }

        //Returns specified parent object, if found, if name is empty, then navigate to top
        private DependencyObject GetParentDependancyObject(DependencyObject child, string name)
        {
            try
            {
                //We need the logical tree to get our parent
                DependencyObject parent = LogicalTreeHelper.GetParent(child);
                DependencyObject lastparent = null;

                //Is the parent our specified control?
                if (name != "" && parent.GetType().ToString() == name) return parent;

                //No, process further
                while (parent != null)
                {
                    string s = parent.GetType().ToString();
                    if (s == name && name != "") return parent;
                    parent = LogicalTreeHelper.GetParent(parent);
                    if (parent != null) lastparent = parent;
                }
                //Return results
                if (name != "") return null;
                else return lastparent;
            }
            catch
            {
                return null;
            }
        }
    }
}

这是我从别人那里得到的代码,并且是我自己修改的代码。我想要做的是通过检查用于创建表单的类别(隐藏属性)并应用不同的验证规则集来进一步扩展它。简单来说,如果表单不是模板模式而category ='Server'应用这些,如果任何其他类别而不是模板模式应用默认值。我以为我可以使用一个简单的if else在私有void FormControl_DataContextChanged(对象发送者,DependencyPropertyChangedEventArgs e部分,但我一直得到验证错误)。 对于这些东西我是新手,所以我希望这不会是一个太复杂的请求。 感谢

0 个答案:

没有答案