Mahapps.metro: Validation Message is displayed twice when Target Framework is .Net Framework 4.5 or 4.6.1

Created on 6 Jun 2016  路  2Comments  路  Source: MahApps/MahApps.Metro

What steps will reproduce this issue?

  1. Create a Catel Wpf Application targeting .Net Framework 4.5 or 4.6.1
  2. Create a model class with a Required data annotation on its "Name" property.
  3. Map the model and it property on the main viewmodel.
  4. Insert a TextBox Control in the main view with ValidatesOnDataErrors=True in the text binding.

net461validationmessage

Expected outcome

net40validationmessage

The expected behaviour occurs If the sample application is written with .Net 4.0.

If I remove ValidatesOnDataErrors=True from the text binding then It displays the validation message once in the application written in .Net 4.61 but it never displays in the version written with .Net 4.0.

I am wondering if Its something wrong on my styles and DataTemplate in the App.xaml resources.

Both applications are attached in this message.

ValidationMessageSolution.zip

Environment

  • MahApps.Metro v1.2.4.0
  • Windows 10
  • Visual Studio 2015
  • .NET Framework 4.6.1

Most helpful comment

I am very grateful for your answer @punker76 . I was not aware of the new INotifyDataErrorInfo interface since we started to develop with .Net 4.5 recently.

My problem was that I have a nuget dependency that contains a couple of user controls with ValidatesOnDataErrors=True set in n the xaml and I needed to solve this conflict with our .Net 4.0 applications.

So I decided to write an specific Binding implementation just for those user controls. Taking into account that this dependency is a multi-target nuget package I wrote a conditional compilation to set ValidatesOnDataErrors=True only for .Net 4.0 assemblies:

public class Binding : System.Windows.Data.Binding
    {

        private void initialize()
        {
#if NET40
            ValidatesOnDataErrors = true;
#endif
        }

        public Binding() : base()
        {
            initialize();
        }

        public Binding(string path) : base(path)
        {
            initialize();
        }
    }

Xaml changes:

<catel:UserControl
  xmlns:b="clr-namespace:MyAssembly.Bindings">
    <TextBox Text="{b:Binding name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</catel:UserControl>

All 2 comments

@llgarrido I think this is caused by the fact, that we have 2 notifies since 4.5 ValidatesOnNotifyDataErrors and ValidatesOnDataErrors So if both are True we see the error twice. You can handle this by setting

ValidatesOnNotifyDataErrors=True, ValidatesOnDataErrors=False

or

ValidatesOnNotifyDataErrors=False, ValidatesOnDataErrors=True

MahApps understands also ValidatesOnNotifyDataErrors

I am very grateful for your answer @punker76 . I was not aware of the new INotifyDataErrorInfo interface since we started to develop with .Net 4.5 recently.

My problem was that I have a nuget dependency that contains a couple of user controls with ValidatesOnDataErrors=True set in n the xaml and I needed to solve this conflict with our .Net 4.0 applications.

So I decided to write an specific Binding implementation just for those user controls. Taking into account that this dependency is a multi-target nuget package I wrote a conditional compilation to set ValidatesOnDataErrors=True only for .Net 4.0 assemblies:

public class Binding : System.Windows.Data.Binding
    {

        private void initialize()
        {
#if NET40
            ValidatesOnDataErrors = true;
#endif
        }

        public Binding() : base()
        {
            initialize();
        }

        public Binding(string path) : base(path)
        {
            initialize();
        }
    }

Xaml changes:

<catel:UserControl
  xmlns:b="clr-namespace:MyAssembly.Bindings">
    <TextBox Text="{b:Binding name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</catel:UserControl>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

punker76 picture punker76  路  4Comments

johanneswanzek picture johanneswanzek  路  3Comments

robertmuehsig picture robertmuehsig  路  3Comments

Foxtrek64 picture Foxtrek64  路  4Comments

barryBithead picture barryBithead  路  5Comments