即使未保存在数据库中,列表视图文本框值也不会恢复为原始值

发布于 2024-11-26 19:08:52 字数 6483 浏览 2 评论 0原文

我在列表视图中有一个文本框,用户可以在其中编辑它,itemsource 是 obervablecollection。一切工作正常,除了当用户编辑文本框的值并按下取消而不是保存时,他关闭窗口并再次打开它时,新值在应该返回到原始值时保留。事实上,我直接从存储库数据库填充它,所以我不确定为什么它没有恢复到原始值。有人知道我的代码有什么问题吗?我缺少什么?谢谢。

<ListView Name="lvNeutralEduc" Grid.Column="0" Grid.Row="0"   BorderBrush="Transparent"    Height="Auto" MaxHeight="300" ScrollViewer.HorizontalScrollBarVisibility="Auto">
                    <ListView.View>
                        <GridView>
                            <GridView.Columns>
                                <GridViewColumn Header="Family Law Course" Width="Auto" >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox IsChecked="{Binding Path=IsFamilyLawCourse}" HorizontalAlignment="Center" Checked="CheckBox_Checked" Unchecked="CheckBox_Checked" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Course Title" Width="200" >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Text="{Binding Path=ContinuingType, Mode=TwoWay}" Width="200" TextChanged="TextBox_TextChanged" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Course Date"  Width="110">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <useable:MaskedDatePicker DateValue="{Binding Path=CourseDate, Mode=TwoWay}"  Width="110" PreviewTextInput="MaskedDatePicker_PreviewTextInput" GotFocus="MaskedDatePicker_GotFocus" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Course Expiration"  Width="110">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <useable:MaskedDatePicker DateValue="{Binding Path=CourseExpiration, Mode=TwoWay}"  Width="110" PreviewTextInput="MaskedDatePicker_PreviewTextInput"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView.Columns>
                        </GridView>
                    </ListView.View>
                </ListView>

背后代码:

 public ObservableCollection<tblContinuingEducationHistory> EducNeutralList
    {
        get { return _educNeutrallist; }
        set
        {
            if (_educNeutrallist != value)
            {
                _educNeutrallist = value;
                OnPropertyChanged("EducNeutralList");
            }
        }
    }
//constructor
public NeutralContinuingEducHistory(tblNeutralFileMaint currentNeutralFM)
    {
        isAttyGal = false;   
        OtherCmdText = "Print Report";
        PrintCmdVisibility = Visibility.Visible;
        OtherCmdVisibility = Visibility.Collapsed;
        AttyGALFormVisibility = Visibility.Collapsed;
        ToggleVisibility();

        InitializeComponent();

        if (currentNeutralFM != null)
        {

            CurrentNeutralFM = currentNeutralFM;
            txtMemberName.Text = CurrentNeutralFM.FullName;
            var items = FileMaintenanceBusiness.Instance.GetManyNeutralFMContEducHistoryInfobyKeyword(currentNeutralFM.NeutralID, "NeutralID");//FileMaintenanceBusiness.Instance.GetManyNeutralFMContEducHistoryInfobyKeyword(currentNeutralFM.NeutralID, "NeutralID")!=null ? FileMaintenanceBusiness.Instance.GetManyNeutralFMContEducHistoryInfobyKeyword(currentNeutralFM.NeutralID, "NeutralID") : null);//currentNeutralFM.tblContinuingEducationHistories);
            EducNeutralList = items != null ? new ObservableCollection<tblContinuingEducationHistory>(items) : new ObservableCollection<tblContinuingEducationHistory>(CurrentNeutralFM.tblContinuingEducationHistories);
            countOrig = EducNeutralList != null ? EducNeutralList.Count : 0;
            lvNeutralEduc.ItemsSource = EducNeutralList;
        }

        this.DataContext = this;
    }

填充 get 方法的业务层:

 public IList<tblContinuingEducationHistory> GetManyNeutralFMContEducHistoryInfobyKeyword(string keyword, string by)
    {
        try
        {
            return _continuingEducationHistoryRepository.SelectManyByColumnKeyword(keyword, by);
        }
        catch (Exception ex)
        {

        }
        return null;
    }

EF 的存储库:

 public IList<TEntity> SelectManyByColumnKeyword(string Key, string columnName)
    {

        // First we define the parameter that we are going to use the clause. 
        var xParam = Expression.Parameter(typeof(TEntity), typeof(TEntity).Name);
        MemberExpression leftExpr = MemberExpression.Property(xParam, columnName);
        Expression rightExpr = Expression.Constant(Key);
        BinaryExpression binaryExpr = MemberExpression.Equal(leftExpr, rightExpr);
        //Create Lambda Expression for the selection 
        Expression<Func<TEntity, bool>> lambdaExpr =
        Expression.Lambda<Func<TEntity, bool>>(binaryExpr,
        new ParameterExpression[] { xParam,  });
        //Searching ....
        IList<TEntity> resultCollection = ((IADRRepository<TEntity, TContext>)this).SelectAll
                    (new Specification<TEntity>(lambdaExpr));
        if (null != resultCollection && resultCollection.Count() > 0)
        {
            //return valid single result
            return resultCollection;
        }//end if 
        return null;
    }

I have a textbox inside a listview in which user can edit it, itemsource is obervablecollection. Everything is working well except that when user edits the value of textbox and pressed cancel insted of save, and he closes the window and opens it again, the new value remains when it should go back to it's orginal value. Infact, I am populating it directly from repository db so I am not sure why it is not reverting back to original value. Anyone knows what is wrong with my code? WHat am I missing? thanks.

<ListView Name="lvNeutralEduc" Grid.Column="0" Grid.Row="0"   BorderBrush="Transparent"    Height="Auto" MaxHeight="300" ScrollViewer.HorizontalScrollBarVisibility="Auto">
                    <ListView.View>
                        <GridView>
                            <GridView.Columns>
                                <GridViewColumn Header="Family Law Course" Width="Auto" >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox IsChecked="{Binding Path=IsFamilyLawCourse}" HorizontalAlignment="Center" Checked="CheckBox_Checked" Unchecked="CheckBox_Checked" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Course Title" Width="200" >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Text="{Binding Path=ContinuingType, Mode=TwoWay}" Width="200" TextChanged="TextBox_TextChanged" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Course Date"  Width="110">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <useable:MaskedDatePicker DateValue="{Binding Path=CourseDate, Mode=TwoWay}"  Width="110" PreviewTextInput="MaskedDatePicker_PreviewTextInput" GotFocus="MaskedDatePicker_GotFocus" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Course Expiration"  Width="110">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <useable:MaskedDatePicker DateValue="{Binding Path=CourseExpiration, Mode=TwoWay}"  Width="110" PreviewTextInput="MaskedDatePicker_PreviewTextInput"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView.Columns>
                        </GridView>
                    </ListView.View>
                </ListView>

Code behind:

 public ObservableCollection<tblContinuingEducationHistory> EducNeutralList
    {
        get { return _educNeutrallist; }
        set
        {
            if (_educNeutrallist != value)
            {
                _educNeutrallist = value;
                OnPropertyChanged("EducNeutralList");
            }
        }
    }
//constructor
public NeutralContinuingEducHistory(tblNeutralFileMaint currentNeutralFM)
    {
        isAttyGal = false;   
        OtherCmdText = "Print Report";
        PrintCmdVisibility = Visibility.Visible;
        OtherCmdVisibility = Visibility.Collapsed;
        AttyGALFormVisibility = Visibility.Collapsed;
        ToggleVisibility();

        InitializeComponent();

        if (currentNeutralFM != null)
        {

            CurrentNeutralFM = currentNeutralFM;
            txtMemberName.Text = CurrentNeutralFM.FullName;
            var items = FileMaintenanceBusiness.Instance.GetManyNeutralFMContEducHistoryInfobyKeyword(currentNeutralFM.NeutralID, "NeutralID");//FileMaintenanceBusiness.Instance.GetManyNeutralFMContEducHistoryInfobyKeyword(currentNeutralFM.NeutralID, "NeutralID")!=null ? FileMaintenanceBusiness.Instance.GetManyNeutralFMContEducHistoryInfobyKeyword(currentNeutralFM.NeutralID, "NeutralID") : null);//currentNeutralFM.tblContinuingEducationHistories);
            EducNeutralList = items != null ? new ObservableCollection<tblContinuingEducationHistory>(items) : new ObservableCollection<tblContinuingEducationHistory>(CurrentNeutralFM.tblContinuingEducationHistories);
            countOrig = EducNeutralList != null ? EducNeutralList.Count : 0;
            lvNeutralEduc.ItemsSource = EducNeutralList;
        }

        this.DataContext = this;
    }

Business layer in populating the get method:

 public IList<tblContinuingEducationHistory> GetManyNeutralFMContEducHistoryInfobyKeyword(string keyword, string by)
    {
        try
        {
            return _continuingEducationHistoryRepository.SelectManyByColumnKeyword(keyword, by);
        }
        catch (Exception ex)
        {

        }
        return null;
    }

Repository for EF:

 public IList<TEntity> SelectManyByColumnKeyword(string Key, string columnName)
    {

        // First we define the parameter that we are going to use the clause. 
        var xParam = Expression.Parameter(typeof(TEntity), typeof(TEntity).Name);
        MemberExpression leftExpr = MemberExpression.Property(xParam, columnName);
        Expression rightExpr = Expression.Constant(Key);
        BinaryExpression binaryExpr = MemberExpression.Equal(leftExpr, rightExpr);
        //Create Lambda Expression for the selection 
        Expression<Func<TEntity, bool>> lambdaExpr =
        Expression.Lambda<Func<TEntity, bool>>(binaryExpr,
        new ParameterExpression[] { xParam,  });
        //Searching ....
        IList<TEntity> resultCollection = ((IADRRepository<TEntity, TContext>)this).SelectAll
                    (new Specification<TEntity>(lambdaExpr));
        if (null != resultCollection && resultCollection.Count() > 0)
        {
            //return valid single result
            return resultCollection;
        }//end if 
        return null;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涙—继续流 2024-12-03 19:08:52

我想通了。我在我的存储库中做了一些更改。 context.Refresh(RefreshMode.StoreWins, resultCollection); 解决了它。

 if (null != resultCollection && resultCollection.Count() > 0)
        {
            //return valid single result
           context.Refresh(RefreshMode.StoreWins, resultCollection);
           return resultCollection;
        }

I figured it out. I made some changes in my repository. context.Refresh(RefreshMode.StoreWins, resultCollection); solved it.

 if (null != resultCollection && resultCollection.Count() > 0)
        {
            //return valid single result
           context.Refresh(RefreshMode.StoreWins, resultCollection);
           return resultCollection;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文