IDataErrorInfo - nicht zu sehen, keine Fehlermeldung, obwohl man angenommen wird

Habe ich ItemType realisiert, dass alles, was man brauchen würde für die Validierung mit Hilfe von IDataErrorInfo-Schnittstelle:

#region IDataErrorInfo implementation
        //WPF doesn't need this one
        public string Error
        { get { return null; } }

        public string this[string propertyName]
        {
            get { return GetValidationError(propertyName); }
        }
        #endregion

        #region Validation
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                {
                    if (GetValidationError(property) != null)
                    {
                        return false;
                    }
                }

                return true;
            }
        }

        static readonly string[] ValidatedProperties =
        {
            "Name"
        };

        private string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "Name":
                    error = ValidateName();
                    break;
                default:
                    Debug.Fail("Unexpected property being validated on Customer: " + propertyName);
                    break;
            }

            return error;
        }

        string ValidateName()
        {
            if (!IsStringMissing(Name))
            {
                return "Name can not be empty!";
            }

            return null;
        }

        static bool IsStringMissing(string value)
        {
            return string.IsNullOrEmpty(value) ||
                   value.Trim() == String.Empty;
        }
        #endregion

ItemType umwickelt mit ItemViewModel. Auf der ItemViewModel ich habe einen Befehl für, wenn ein Benutzer auf den Speichern-Button:

public ICommand SaveItemType
        {
            get
            {
                if (saveItemType == null)
                {
                    saveItemType = new RelayCommand(() => Save());
                }

                return saveItemType;
            }
        }

Dann in der Detailansicht, ich habe folgende xaml-code:

<TextBlock Text="Name:" />
<TextBox Grid.Column="1" Name="NameTextBox" Text="{Binding Name, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                         Validation.ErrorTemplate="{x:Null}" />

<ContentPresenter Grid.Row="13" Grid.Column="2"
                  Content="{Binding ElementName=NameTextBox, Path=(Validation.Errors).CurrentItem}" />

folgende Architektur geht auf (es ist nicht klar, aber die form ist eigentlich eine unabhängige xaml-Datei (User Control), wo der datacontext des Gitters in die form eingestellt ist, um die ObservableCollection):

<Grid DataContext="{Binding Items}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

IDataErrorInfo - nicht zu sehen, keine Fehlermeldung, obwohl man angenommen wird

Das problem, das ich habe, ist, dass die Fehlermeldungen werden nicht angezeigt. Wenn ich den Haltepunkt und prüfe, ob es korrekt überprüft wird und wenn ich irgendwelche Fehlermeldungen, dann Mach ich Sie habe. Aber irgendwie ist die Fehlermeldung nicht kommen in der xaml.

Was ist das fehlende Stück im puzzle?

BEARBEITEN - DAS FEHLENDE STÜCK

Recht, so, wie es war, war Folgendes:

IDataErrorInfo implementiert habe ich auf mein Modell, aber nicht auf dem ViewModel, dass umschließt das Modell. Was ich zu tun hatte, war auch die Umsetzung der IDataErrorInfo-Schnittstelle in das ViewModel, und bekommen es aus dem Modell.

ViewModel Implementierung von IDataErrorInfo:

{ get { return (ItemType as IDataErrorInfo).Error; } }

public string this[string propertyName]
{
  get
  {
    return (ItemType as IDataErrorInfo)[propertyName];
  }
}
InformationsquelleAutor DerMeister | 2011-04-26
Schreibe einen Kommentar