WPF Validation & Saving Problem

WPF Validation & Saving Problem

Old forum URL: forums.lhotka.net/forums/t/3492.aspx


jtnjtnjtn posted on Thursday, September 06, 2007

I've started writing my WPF app based on the Project Tracker example. 
Having difficulty getting validation and creation of new objects to work.  Advice on where to start loking would be greatly appreciated.
I have a simple edit form for one of my objects which simply shows the Id and 2 text fields.

The behaviour I get in the edit form is as follows:

1. Create a New Object

IsSavable = false
IsValid = false
IsDirty = true
IsNew = true


Putting the focus in to a text box puts a red line around both text boxes which I presume indicates an invalid value.  This happens whether or not the value is valid.  The Cancel button becomes enabled and the Save button stays disabled.  The 4 Is* values remain unchanged.
Entering valid data does not remove the red lines around the text boxes.
Unable to proceed any further to create a new object.


2. Edit an Existing Object

IsSavable = false
IsValid = true
IsDirty = false
IsNew = false


The form is initially displayed with the correct values.
Changing either of the text box values sets IsSavable & IsDirty to true.
Changing either or both of the text box values to invalid data does not set IsValid to false and does not put the red lines around the invalid text box(es).
Both the Save and Cancel buttons are enabled.
Changes are saved correctly.

The XAML follows the ProjectTracker sample.
As far as I can see the only ways I've departed from the ProjectTracker sample are:
* Not using WCF
* I've created a couple of base classes for common code.

    public class BaseUserControl : UserControl, IRefresh
    {

        public BaseUserControl()
        {
            this.Loaded += new System.Windows.RoutedEventHandler(BaseUserControl_Loaded);
        }

        void BaseUserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ApplyAuthorization();
        }

        void IRefresh.Refresh()
        {
            ApplyAuthorization();
        }

        protected virtual void ApplyAuthorization()
        {
        }

        protected virtual void DataChanged(object sender, EventArgs e)
        {
            CslaDataProvider dp = sender as CslaDataProvider;
            if (dp.Error != null)
                MessageBox.Show(dp.Error.ToString(), Properties.Resources.TextErrorData, MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }
    }

    public class BaseEditForm : BaseUserControl
    {
        private Guid _id = Guid.Empty;
        protected static string _businessObjectName = string.Empty;

        protected Guid Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public BaseEditForm()
            :base()
        {
        }

        protected void CreateEvents()
        {
            CslaDataProvider dp = this.FindResource(_businessObjectName) as CslaDataProvider;
            dp.DataChanged += new EventHandler(DataChanged);
        }

    }

    // This is the form for my business object which is simply Id, CountryName and Country Abbreviation.
    public partial class CountryEdit : BaseEditForm
    {
        static CountryEdit()
        {
            _businessObjectName = "Country";
        }

        public CountryEdit()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(CountryEdit_Loaded);
            CreateEvents();
        }

        public CountryEdit(Guid id)
            : this()
        {
            Id = id;
        }

        void CountryEdit_Loaded(object sender, RoutedEventArgs e)
        {
            CslaDataProvider dp = this.FindResource(_businessObjectName) as CslaDataProvider;
            using (dp.DeferRefresh())
            {
                dp.FactoryParameters.Clear();
                if (Id.Equals(Guid.Empty))
                {
                    dp.FactoryMethod = "New";
                }
                else
                {
                    dp.FactoryMethod = "Get";
                    dp.FactoryParameters.Add(Id);
                }
            }
            if (dp.Data != null)
                SetTitle((Country)dp.Data);
            else
                MainWindow.ShowEditControl(new CountryListControl());
        }

        protected override void ApplyAuthorization()
        {
            this.AuthPanel.Refresh();
            if (Country.CanEditObject())
            {
            }
            else
            {
                ((CslaDataProvider)this.FindResource(_businessObjectName)).Cancel();
            }
        }
    }

Copyright (c) Marimer LLC