0

I created a WPF application by using MVVM design pattern. The MainView has a tabcontrol with two items each of them has a different View / ViewModel as showed in the following code

    <TabControl>
        <TabItem Header="Test 2" DataContext="{Binding CurrentTest2ViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <AdornerDecorator>
                <local:Test2View></local:Test2View>
            </AdornerDecorator>
        </TabItem>             
            <TabItem Header="Test 1" DataContext="{Binding CurrentTest1ViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <AdornerDecorator>
                <local:Test1View></local:Test1View>
            </AdornerDecorator>
        </TabItem>
    </TabControl>

In one of the two ViewModels I used IDataErrorInfo Interface to validate properties. The corresponding View has the following Code

<StackPanel Orientation="Vertical">
    <CheckBox HorizontalAlignment="Left" Width="80" Content="IsRed" IsChecked="{Binding IsRed, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" IsEnabled="{Binding IsEnabled}">
        <Validation.ErrorTemplate>
            <ControlTemplate>
                <StackPanel Orientation="Horizontal">
                    <AdornedElementPlaceholder />
                    <Label Content="{Binding Path=ErrorContent}" ToolTip="{Binding Path=ErrorContent}" />
                </StackPanel>
            </ControlTemplate>
        </Validation.ErrorTemplate>
    </CheckBox>
    <CheckBox HorizontalAlignment="Left" Width="80" Content="IsGreen" IsChecked="{Binding IsGreen, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" IsEnabled="{Binding IsEnabled}">
        <Validation.ErrorTemplate>
            <ControlTemplate>
                <StackPanel Orientation="Horizontal">
                    <AdornedElementPlaceholder />
                    <Label Content="{Binding ErrorContent}" ToolTip="{Binding ErrorContent}" />
                </StackPanel>
            </ControlTemplate>
        </Validation.ErrorTemplate>
    </CheckBox>
</StackPanel>

When the first time the item is showed the validation discover an incorrect value for some property as I can see through breakpoints, but the error message does not appear on the screen. To reproduce the error message I need to select before correct values and after that when the values are not valid the error messages appears.

My question are:

1) How can I reproduce the error messages of validation from the first time that the item is showed on the screen? 2) Why do I have no problems if I use only the main view and not different views for any items?

Thank you very much in advance.

  • What's the code for ErrorContent? Do you implement [INotifyPropertyChanged](https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx) for it? – Peter Feb 06 '16 at 23:37
  • I implemented IDataErrorInfo Interface. The ErrorContent comes from it. – Filippo Riccio Feb 06 '16 at 23:43
  • IDataErrorInfo does not inherit from INotifyPropertyChanged, you need INotifyPropertyChanged interface for property-binding to update. Please read above link for details. – Peter Feb 06 '16 at 23:45
  • Of Course, I implemented INotifyPropertyChanged. – Filippo Riccio Feb 06 '16 at 23:54
  • Could you post the getter-setter code for the property ErrorContent? – Peter Feb 07 '16 at 00:23
  • What do you mean by `I need to select before correct values` ? – AnjumSKhan Feb 07 '16 at 09:27
  • Which property you are validating ? – AnjumSKhan Feb 07 '16 at 09:39
  • @Peter the property ErrorContent Comes from IDataErrorInfo Interface that in my Code is implemented as public string this[string columnName] { get { if (string.Equals(columnName, nameof(IsRed))) return TestNumbers.RedTest <= 5 ? "Red has error." : string.Empty; if (string.Equals(columnName, nameof(IsGreen))) return TestNumbers.GreenTest > 5 ? "Green has an error." : string.Empty; return string.Empty; } } public string Error { get; } – Filippo Riccio Feb 07 '16 at 17:58
  • @AnijumSKahn: At the beginning I know that a property is not valid, for this reason I wait for an error message from Validation. However this message is not showed. Moreover if the properties are all valid at the beginning and after I change the properties to reproduce some incorrect values, then the Validation works and the error message are showed. – Filippo Riccio Feb 07 '16 at 18:04
  • @FilippoRiccio check this out. http://stackoverflow.com/questions/14023552/how-to-use-idataerrorinfo-error-in-a-wpf-program – Peter Feb 07 '16 at 18:11
  • @Peter I saw the link that you suggested me. Unfortunately it was not useful. My Problem is related to the error messages that are showed only if the property values start with correct values. Otherwise the Validation and the error Messages work well. Moreover I said that if I use only one view for the tabcontrol and the items I do not have any Problem. – Filippo Riccio Feb 07 '16 at 18:56

0 Answers0