1

I'm creating a simple WPF application that needs to display/Edit data from SQL server using Entity Framework Model. I created a small test window to see how things work and i noticed that property changed events and data validation are kind of automatically implemented. here's my xaml:

<Style TargetType="{x:Type Button}">
        <Setter Property="IsEnabled" Value="False"/>
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding ElementName=txtRibbonCoreSize,Path=(Validation.HasError)}" Value="False"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="IsEnabled" Value="True"/>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>

<StackPanel>
    <ac:AutoCompleteBox x:Name="txtPrn1" Width="250" HorizontalAlignment="Left"                                
                           ValueMemberBinding="{Binding Converter={StaticResource prnC}}" 
                            FilterMode="Contains"
                            ItemsSource="{Binding PrinterParams}">
        <ac:AutoCompleteBox.TextBoxStyle>
            <Style TargetType="TextBox">
                <Setter Property="FocusManager.FocusedElement" 
                        Value="{Binding RelativeSource={RelativeSource Self}}" />
            </Style>
        </ac:AutoCompleteBox.TextBoxStyle>
        <ac:AutoCompleteBox.ItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0} - {1}">
                            <Binding Path="Manufacturer"/>
                            <Binding Path="Model"/>
                        </MultiBinding> 
                    </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ac:AutoCompleteBox.ItemTemplate>
    </ac:AutoCompleteBox>
    <TextBox Text="{Binding ElementName=txtPrn1,Path=SelectedItem.MHeight,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <Button x:Name="btn1" Content="Accept" Click="btn1_Click"/>
</StackPanel>

and here's my code:

    //instance of EF datamodel object
    DbEntities db = new DbEntities();
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DataContext = db;
    }

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        db.SaveChanges();
    }

MHeight is an integer and if i put a non integer value in the textbox it's borders becomes red and the button becomes disabled (according to the validation style above). if i click on the button the new data is saved correctly.
does the EF model implements the INotifyPropertyChanged and the IDataErrorInfo interfaces?

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Yoav
  • 3,326
  • 3
  • 32
  • 73

1 Answers1

3

For the INotifyPropertyChanged yes it's already implemented , but IDataErrorInfo you have to implement it your self.
Your Entities inherit from EntityObject which it self inherits from StructuralObject which finally implements INotifyPropertyChanged

HichemSeeSharp
  • 3,240
  • 2
  • 22
  • 44
  • well i guess there is some kind of error validation mechanism since when clicking the button the `db.SaveChanges()` command is skipped if there are errors in the edited data. no exception, it just doesn't happen. – Yoav Jul 01 '12 at 21:54
  • 1
    I think you are confusing between Data Validation and ExceptionValidation. DataValidation can be personalized ex: an email without @ character can raise Data Validation error.for this you ought to use IDataErrorInfo.but ExceptionValidation are any data that would cause an exception when saved to database, ex: an int property could not have non numeric characters and would not save to database. EF handls Exception Validation errors – HichemSeeSharp Jul 02 '12 at 00:07