简体   繁体   English

当ObservableCollection绑定更改时,Listview不会更新

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

I'm new to WPF and having some trouble with ListView's. 我是WPF的新手,并且ListView遇到了一些麻烦。 I've created a list view to visually display data in an Observable collection to users. 我创建了一个列表视图,以可视方式向用户显示Observable集合中的数据。 The only problem is the ListView doesn't update when values within the ObservableCollection are changed. 唯一的问题是,当ObservableCollection中的值更改时,ListView不会更新。 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 列表视图窗口的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>

The c# associated with that is simple 与之关联的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;
    }

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. 我相信ListView正确地绑定到ObservableCollection,但是似乎找不到错误或合适的解决方法。

CycleTimer class: 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

Impliment INotifyPropertyChanged Interface with your model CycleTimer to get the changes get affected. 暗示INotifyPropertyChanged与模型CycleTimer的接口,以使更改受到影响。

"Mark W" have already commented on this. “ Mark W”已经对此发表了评论。

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

ObservableCollection Class ObservableCollection类

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>> 我有一个对象的ObservableCollection作为子属性..即使您实现INotifyPropertyChanged >>,它的作用也与您抱怨的一样。

Solved by just this : 解决了这个问题:

Obj.ObsCol = new ObservableCollection<Obj.ObsCol>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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