简体   繁体   中英

Listview doesn't update when the ObservableCollection its bound to changes

I'm new to WPF and having some trouble with ListView's. I've created a list view to visually display data in an Observable collection to users. The only problem is the ListView doesn't update when values within the ObservableCollection are changed. If an element is added or removed that is displayed correctly, however. Closing and reopening the window will also cause it to display the correct data.

The xaml for the list view window is

<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>

The c# associated with that is simple

    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;
    }

And finally the view model...

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; }
    }
}

I believe the ListView is bound to the ObservableCollection correctly, but can't seem to find whats wrong or a suitable work around.

CycleTimer class:

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

Impliment INotifyPropertyChanged Interface with your model CycleTimer to get the changes get affected.

"Mark W" have already commented on this.

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

ObservableCollection Class

Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

so it wont notify when you change the model property.

I had an ObservableCollection of objects as a sub property.. which does the same as you complain.. even though implementing INotifyPropertyChanged>>

Solved by just this :

Obj.ObsCol = new ObservableCollection<Obj.ObsCol>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM