Listview未在WPF

时间:2017-06-16 22:40:35

标签: c# wpf listview mvvm

我有一个列表视图,显示搜索框的搜索结果。加载窗口时,数据库中的所有数据都显示在listview中 - "这很好"。

但是当我搜索事物时,视图模型中的变量会更改并存储新数据,但listview不会显示变量中的新数据。

listview的XAML代码

<ListView x:Name="itemsListView" ScrollViewer.CanContentScroll="True"
                  ItemsSource="{Binding Rows, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
              Grid.Row="1"
              HorizontalAlignment="Stretch"  
              VerticalAlignment="Stretch"
              Margin="40,10,40,10"
              FlowDirection="RightToLeft" IsSynchronizedWithCurrentItem="True" SelectedIndex="1">
    <ListView.View>
        <GridView AllowsColumnReorder="False">
            <GridViewColumn DisplayMemberBinding="{Binding BankName}" 
                            Width="250"
                            Header="bank name"/>
            <GridViewColumn DisplayMemberBinding="{Binding Credit}" 
                            Header="credit"
                            Width="100"/>
            <GridViewColumn DisplayMemberBinding="{Binding IsActive}" 
                            Header="statue"
                            Width="100"/>
            <GridViewColumn DisplayMemberBinding="{Binding Notes}" 
                            Header="notes"
                            Width="230"/>
        </GridView>
    </ListView.View>
</ListView>

查看型号代码

class BanksVM 
{
    private ObservableCollection<BanksListVM> m_Rows;

    public ObservableCollection<BanksListVM> Rows
    {
        get { return m_Rows; }
        set { m_Rows = value; }
    }

    private string _BanksSearchBox;

    public string BanksSearchBox
    {
        get { return _BanksSearchBox; }
        set {
            _BanksSearchBox = value;
            SearchInBanksMethod();
        }
    }

    //set all data to listview
    public BanksVM()
    {
        Rows = new ObservableCollection<BanksListVM>();
        Rows.Clear();

        List < Banks > bankslist = new List<Banks>();
        Banks banks = new Banks();
        bankslist = banks.GetAllBanks();

        foreach (Banks b in bankslist)
        {
            string IsActive = "";

            if (b.IsActive == true)
                IsActive = "open";
            else
                IsActive = "closed";

            Rows.Add(new BanksListVM
            {
                ID = b.ID,
                BankName = b.BankName,
                IsActive = IsActive,
                Credit = b.Credit,
                Notes = b.Notes
            });
        }
    }

    // refresh listview method
    public void SearchInBanksMethod() { 
        Rows = new ObservableCollection<BanksListVM>();
        Rows.Clear();

        List<Banks> bankslist = new List<Banks>();
        search Search = new search();
        bankslist = Search.SearchInBanks(_BanksSearchBox);

        foreach (Banks b in bankslist)
        {
            string IsActive = "";

            if (b.IsActive == true)
                IsActive = "open";
            else
                IsActive = "closed";

            Rows.Add(new BanksListVM
            {
                ID = b.ID,
                BankName = b.BankName,
                IsActive = IsActive,
                Credit = b.Credit,
                Notes = b.Notes
            });
        }
    }
}

1 个答案:

答案 0 :(得分:1)

重新分配ObservableCollection时,不会通知Binding有关新属性值的信息。

SearchInBanksMethod中,删除

Rows = new ObservableCollection<BanksListVM>();

并且仅使用

Rows.Clear();