使datagrid addrow功能与实体框架一起使用

时间:2016-07-04 07:49:07

标签: c# wpf entity-framework datagrid wpfdatagrid

我正在编写这个简单的应用程序,它有两个主要表格,一个用于图片信息,另一个用于有关图片的参数列表,如下图所示 enter image description here

我希望能够在没有任何其他形式的情况下添加到参数列表中,但是当我添加一行时,它会正确地将另一行添加到数据库中,但保存输入的文本不起作用。(我在“RowEditEnding”中保存更改事件) 这是XAML:

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.5*"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid Margin="10">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="50"/>
            </Grid.RowDefinitions>
            <ListBox Name="lstPicture" Grid.Column="0" ItemsSource="{Binding pictures, Mode=OneWayToSource}" DisplayMemberPath="Name" SelectedValuePath="ID">
            </ListBox>
            <Button Grid.Row="1" Content="جدید" Height="Auto" Margin="0,0,0,10"/>
        </Grid>
        <GroupBox Header="اطلاعات تصویر" Grid.Column="1" Margin="0,10,10,10">
            <!--Image Data Grid-->
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="200px"/>
                </Grid.ColumnDefinitions>
                <!--Image Data Right Grid-->
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="100px"/>
                    </Grid.RowDefinitions>
                    <materialDesign:Card Margin="10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                        <Image Name="imgPreview" Margin="5" Source="{Binding SelectedItem.URL, ElementName=lstPicture}" MouseLeftButtonUp="Image_MouseLeftButtonUp">
                        </Image>
                    </materialDesign:Card>
                    <!--Image Data Fields-->
                    <Grid Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100px"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="VerticalAlignment" Value="Center"/>
                                <Setter Property="Margin" Value="5"/>
                            </Style>
                            <Style BasedOn="{StaticResource MaterialDesignTextBox}" TargetType="TextBox">
                                <Setter Property="Margin" Value="5"/>
                            </Style>
                            <Style BasedOn="{StaticResource MaterialDesignRaisedButton}" TargetType="Button">
                                <Setter Property="Margin" Value="5"/>
                            </Style>
                        </Grid.Resources>
                        <TextBlock Text="نام تصویر"/>
                        <TextBox Grid.Column="1" Text="{Binding SelectedItem.Name, ElementName=lstPicture}"/>
                        <TextBlock Text="عکاس" Grid.Row="1"/>
                        <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding SelectedItem.Author, ElementName=lstPicture}"/>
                        <TextBlock Text="انتخاب تصویر" Grid.Row="2"/>
                        <Button Name="btnChooseImage" Content="..." Grid.Column="1" Grid.Row="2" Height="Auto" Click="btnChooseImage_Click"/>
                    </Grid>

                </Grid>
                <DataGrid Name="dgrdQuestions" Grid.Column="1" Margin="10" ItemsSource="{Binding SelectedItem.tests, ElementName=lstPicture,Converter={StaticResource ObservableCollectionConverter},UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" CanUserAddRows="True" CellEditEnding="DataGrid_CellEditEnding" RowEditEnding="dgrdQuestions_RowEditEnding" AddingNewItem="DataGrid_AddingNewItem">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding Title}" Header="عنوان" Width="1*"/>
                    </DataGrid.Columns>
                    <DataGrid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Name="btnAddQuestion" Header="جدید" Click="btnAddQuestion_Click"/>
                            <MenuItem Name="btnDeleteQuestion" Header="حذف" Click="btnDeleteQuestion_Click"/>
                        </ContextMenu>
                    </DataGrid.ContextMenu>
                </DataGrid>
            </Grid>
        </GroupBox>
    </Grid>

这是代码:

public AdminPanel()
{
    InitializeComponent();
    lstPicture.ItemsSource = App.Entities.pictures.ToList();
}

private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    App.Entities.SaveChanges();
}

private void DataGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
    App.Entities.tests.Add(new test { pictureID = (int)lstPicture.SelectedValue });
    App.Entities.SaveChanges();
}

p.s:我总是遇到WPF绑定和实体框架的问题,这让我用hacky方法来解决它。如果有人向我解释使用不会导致此类问题的绑定的正常方法,我将不胜感激

1 个答案:

答案 0 :(得分:0)

由于EF不知道你改变的实体(它是由ModelBinder而不是EF本身改变的,我们需要告诉EF该对象存在并且它是一个修改过的App.Entities.Entry(test).State = EnitityState.Modified;

var datag = (DataGrid) sender;
var t = (Test) datag.SelectedValue;
App.Entities.Entry(test).State = EnitityState.Modified;
App.Entities.SaveChanges();

如果对象因任何原因不在此给定的DbContext中,那么你必须通过Id找到它然后更改它,之后你可以调用App.Entities.SaveChanges();