Search code examples
c#wpfentity-frameworkdata-bindingmultidatatrigger

using EntityFramework model as datacontext in wpf


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?


Solution

  • 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