插入操作后重新绑定listview

本文关键字:绑定 listview 新绑定 操作 插入 | 更新日期: 2023-09-27 18:14:22

我们有一个Listview。在listview中,我们有一个import按钮,用于将数据导入SQL SERVER数据库。

导入数据后,如果我试图重新绑定Listview,它不会反映更改或重新绑定。

下面是我的代码片段

<Window x:Class="Searching.ImportedKeywords"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ImportedKeywords" Height="418.769" Width="1332.12" Background="#FFFBF6E3">
    <Grid Margin="-1094,0,2,24" RenderTransformOrigin="0.621,0.497">
        <Grid.RowDefinitions>
            <RowDefinition Height="432*"/>
            <RowDefinition Height="350*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="0*"/>
        </Grid.ColumnDefinitions>
        <ListView Name="lvShowSearching" Height="247" VerticalAlignment="Top" RenderTransformOrigin="2.25,3.86" Margin="1100,126,0,0" Grid.RowSpan="2">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="First Name" Width="70" DisplayMemberBinding="{Binding FirstName}" />
                    <GridViewColumn Header="Last Name" Width="70" DisplayMemberBinding="{Binding LastName}" />
                    <GridViewColumn Header="All Words" Width="100" DisplayMemberBinding="{Binding WithAllOfTheWords}" />
                    <GridViewColumn Header="Exact Phrase" Width="100" DisplayMemberBinding="{Binding WithTheExactPhrase}" />
                    <GridViewColumn Header="At Least One Word" Width="150" DisplayMemberBinding="{Binding WithAtLeastOneOfTheseWords}" />
                    <GridViewColumn Header="Without Word" Width="100" DisplayMemberBinding="{Binding WithoutTheWord}" />
                    <!--<GridViewColumn Header="Exact Date" Width="70" DisplayMemberBinding="{Binding ExactDate}" />
                    <GridViewColumn Header="Years" Width="70" DisplayMemberBinding="{Binding StartYear}" />
                    <GridViewColumn Header="Start Date" Width="70" DisplayMemberBinding="{Binding StartDate}" />
                    <GridViewColumn Header="End Date" Width="70" DisplayMemberBinding="{Binding EndDate}" />-->
                    <GridViewColumn Header="Country" Width="70" DisplayMemberBinding="{Binding CountryName}"/>
                    <GridViewColumn Header="State" Width="70" DisplayMemberBinding="{Binding StateName}" />
                    <GridViewColumn Header="City" Width="70" DisplayMemberBinding="{Binding CityName}" />
                    <GridViewColumn Header="Publication" Width="70" DisplayMemberBinding="{Binding PublicationName}" />
                    <GridViewColumn Header="Total Records" Width="100" DisplayMemberBinding="{Binding TotalRecords}" />
                    <GridViewColumn Header="Records Imported" Width="100" DisplayMemberBinding="{Binding TotalRecordsImported}" />
                    <GridViewColumn Header="Status" Width="70" DisplayMemberBinding="{Binding Status}" />
                    <GridViewColumn Header="Records To Import" Width="120">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Width="70" Name="txtRecordsToImport" Text="{Binding RecordsToImport}"></TextBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Import" Width="70">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Click="Import_Click">Import</Button>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <Image HorizontalAlignment="Left" Source="Images/logonav2.png" Height="100" Margin="1100,5,0,0" VerticalAlignment="Top" Width="1190"/>
    </Grid>
</Window>

导入按钮代码:

private void Import_Click(object sender, RoutedEventArgs e)
        {
            DateFilter dateFilter = new DateFilter();
            SearchRequest objSearchRequest = new SearchRequest();
            ListViewItem grvRow = FindVisualParent<ListViewItem>(sender as UIElement);
            if (grvRow != null)
            {
                ClippingSearchKeyword objSearch = ((ClippingSearchKeyword)(grvRow).Content);
                if (objSearch.RecordsToImport == 0)
                {
                    MessageBox.Show("Please enter records to import.");
                    return;
                }
                objSearchRequest.LocationFilter.CitiesString = Convert.ToString(objSearch.CityID);
                objSearchRequest.LocationFilter.CountriesString = Convert.ToString(objSearch.CountryID);
                objSearchRequest.LocationFilter.StatesString = Convert.ToString(objSearch.StateID);
                objSearchRequest.LocationFilter.PublicationsString = Convert.ToString(objSearch.PublicationID);
                objSearchRequest.FirstName = objSearch.FirstName;
                objSearchRequest.LastName = objSearch.LastName;
                objSearchRequest.AllOfTheWordsString = objSearch.WithAllOfTheWords;
                objSearchRequest.WithoutWordsString = objSearch.WithoutTheWord;
                objSearchRequest.ExactPhraseString = objSearch.WithTheExactPhrase;
                objSearchRequest.AnyOfTheWordsString = objSearch.WithAtLeastOneOfTheseWords;
                objSearchRequest.AllOfTheWordsString = objSearch.WithAllOfTheWords;
                if (objSearch.ExactDate != null)
                {
                    dateFilter.SetExactDateFilter(objSearch.ExactDate.Value);
                    objSearchRequest.DateFilters.Add(0, dateFilter);
                }
                else if (objSearch.StartDate != null)
                {
                    dateFilter.SetBetweenDatesFilter(objSearch.StartDate.Value, objSearch.EndDate.Value);
                    objSearchRequest.DateFilters.Add(0, dateFilter);
                }
                else if (true)
                {
                    dateFilter.SetBetweenYearsFilter(objSearch.StartYears.Value, objSearch.EndYear.Value);
                    objSearchRequest.DateFilters.Add(0, dateFilter);
                }
                int count = new AdvancedSearching().SearchContent(objSearchRequest, objSearch.TotalRecords, objSearch.TotalRecordsImported, objSearch.RecordsToImport, objSearch.KeywordID);
                if (count == 0)
                {
                    MessageBox.Show("No record found to import.");
                }
                else
                {
                    MessageBox.Show(count + " Record(s) imported successfully.");
                    BindList();
                }
            }
        }

BindList();是绑定listview的方法。

public void BindList()
 {
    var data = objCommon.GetSearchKeywords();
    lvShowSearching.ItemsSource = data;
 }

也请建议一个更好的方法来设计这个网格。我基本上是一个web开发人员,但一直在WPF工作了2-3天。

插入操作后重新绑定listview

use

<ListView Name="lvShowSearching" Height="247" VerticalAlignment="Top" RenderTransformOrigin="2.25,3.86" Margin="1100,126,0,0" Grid.RowSpan="2" ItemsSource={Binding Path=.}> 

代替

<ListView Name="lvShowSearching" Height="247" VerticalAlignment="Top" RenderTransformOrigin="2.25,3.86" Margin="1100,126,0,0" Grid.RowSpan="2"> 

后面的代码使用

lvShowSearching.UpdateLayout();

lvShowSearching.ItemSource=data