我正在创建一个简单的WPF应用程序,它需要使用Entity Framework Model显示/编辑来自SQL Server的数据。我创建了一个小的测试窗口,看看事情是如何工作的,我注意到属性改变了事件,数据验证是自动实现的。 这是我的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>
这是我的代码:
//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是一个整数,如果我在文本框中放入一个非整数值,它的边框变为红色,按钮变为禁用(根据上面的验证方式)。如果我点击按钮,新数据将被正确保存 EF模型是否实现了INotifyPropertyChanged和IDataErrorInfo接口?
答案 0 :(得分:3)
对于INotifyPropertyChanged
是的,它已经实现了,但是IDataErrorInfo
你必须自己实现它。
您的实体继承自EntityObject
,它自StructuralObject
继承,最终实现INotifyPropertyChanged