当ObservableCollection的绑定发生变化时,Listview不会更新

本文关键字:Listview 更新 变化 ObservableCollection 绑定 | 更新日期: 2023-09-27 18:04:27

我是WPF的新手,在使用ListView时遇到了一些麻烦。我已经创建了一个列表视图,以便可视地向用户显示Observable集合中的数据。唯一的问题是,当ObservableCollection中的值发生变化时,ListView不会更新。但是,如果添加或删除了正确显示的元素。关闭和重新打开窗口也会使它显示正确的数据。

列表视图窗口的xaml是
<Grid>
    <ListView Name="RunningCycleListView" HorizontalAlignment="Left" Height="266" VerticalAlignment="Top" Width="546" ItemsSource="{Binding RunningCycles, BindsDirectlyToSource=True}">
        <ListView.Resources>
            <Style TargetType="{x:Type ListViewItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding OverCycleLogged}" Value="true">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Name}">
                    <GridViewColumnHeader Width="100">
                        <TextBlock Text="Cycle Name"/>
                    </GridViewColumnHeader>
                </GridViewColumn>
                <GridViewColumn DisplayMemberBinding="{Binding AGV.AgvGroup}">
                    <GridViewColumnHeader Width="67">
                        <TextBlock Text="Group"/>
                    </GridViewColumnHeader>
                </GridViewColumn>
                <GridViewColumn DisplayMemberBinding="{Binding AGV.Type}">
                    <GridViewColumnHeader Width="67">
                        <TextBlock Text="Type"/>
                    </GridViewColumnHeader>
                </GridViewColumn>
                <GridViewColumn DisplayMemberBinding="{Binding AGV.AgvID_Number}" Width="90">
                    <GridViewColumnHeader Width="90">
                        <TextBlock Text="ID"/>
                    </GridViewColumnHeader>
                </GridViewColumn>
                <GridViewColumn DisplayMemberBinding="{Binding StartTime}" Width="140">
                    <GridViewColumnHeader Width="140">
                        <TextBlock Text="Start Time"/>
                    </GridViewColumnHeader>
                </GridViewColumn>
                <GridViewColumn DisplayMemberBinding="{Binding MaxTime}">
                    <GridViewColumnHeader Width="75">
                        <TextBlock Text="Max Time"/>
                    </GridViewColumnHeader>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

与之相关的c#是简单的

    public partial class CycleViewerWindow : Window
{
    public CycleViewerVM viewModel;
    public CycleViewerWindow(ObservableCollection<CycleTimer> runningCycles)
    {
        this.viewModel = new CycleViewerVM(runningCycles);
        InitializeComponent();
        this.DataContext = viewModel;
        this.Closing += CycleViewerWindow_Closing;
    }
    void CycleViewerWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        this.Closing -= CycleViewerWindow_Closing;
    }

最后是视图模型…

public class CycleViewerVM
{
    public CycleViewerVM(ObservableCollection<CycleTimer> runningCycles)
    {
        this.RunningCycles = runningCycles;
    }
    private ObservableCollection<CycleTimer> runningCycles;
    public ObservableCollection<CycleTimer> RunningCycles
    {
        get { return runningCycles; }
        set { this.runningCycles = value; }
    }
}

我相信ListView绑定到ObservableCollection正确,但似乎不能找到什么是错误的或一个合适的工作。

CycleTimer类:

public class CycleTimer
{
    #region Constructor(s)
    public CycleTimer(string cycleTimerName, int CycleTimerStartRfid, int CycleTimerStopRfid, int CycleTimerMaxCount, AGVData agv = null)
    {
        this.name = cycleTimerName;
        this.cycleStartRfid = CycleTimerStartRfid;
        this.cycleStopRfid = CycleTimerStopRfid;
        this.maxTime = CycleTimerMaxCount;
        this.agv = agv;
        this.startTime = DateTime.Now;
    }
    #endregion Constructor(s)
    #region Fields and Properties
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private DateTime startTime;
    public DateTime StartTime
    {
        get { return startTime; }
        protected set { startTime = value; }
    }
    private int maxTime;
    public int MaxTime
    {
        get { return maxTime; }
        set { maxTime = value; }
    }
    private AGVData agv;
    public AGVData AGV
    {
        get { return agv; }
    }

    private Boolean overCycleLogged;
    public Boolean OverCycleLogged
    {
        get { return this.overCycleLogged; }
        set { this.overCycleLogged = value; }
    }
    #endregion Fields and Properties
    #region Methods
    public Boolean isOverCycle()
    {
        DateTime maximumTime = this.startTime.AddSeconds(this.maxTime);
        if (DateTime.Now > maximumTime)
        {
            return true;
        }
        return false;
    }
    #endregion Methods

当ObservableCollection的绑定发生变化时,Listview不会更新

实现INotifyPropertyChanged接口与您的模型CycleTimer,以使更改得到影响。

"Mark W"已经对此发表了评论。

https://stackoverflow.com/a/1316417/2470362

ObservableCollection类

表示动态数据集合,在添加、删除项或刷新整个列表时提供通知。

所以当你改变模型属性时它不会通知你

我有一个ObservableCollection的对象作为子属性。这和你抱怨的是一样的…尽管实现了INotifyPropertyChanged>>

解决方法:

Obj.ObsCol = new ObservableCollection<Obj.ObsCol>