我正在创建一个简单的WPF应用程序,它需要使用实体框架模型显示/编辑来自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接口?
发布于 2012-07-02 02:28:41
对于INotifyPropertyChanged,是的,它已经实现了,但是IDataErrorInfo你必须自己实现它。
您的实体继承自EntityObject,自继承自最终实现INotifyPropertyChanged的StructuralObject
https://stackoverflow.com/questions/11282664
复制相似问题