1

我正在创建一个简单的 WPF 应用程序,该应用程序需要使用实体框架模型显示/编辑来自 SQL 服务器的数据。我创建了一个小测试窗口来查看事情是如何工作的,我注意到属性更改事件和数据验证是自动实现的。这是我的xml:

<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 接口?

4

1 回答 1

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

于 2012-07-01T18:28:40.933 回答