我是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
答案 0 :(得分:1)
对您的模型CycleTimer进行INotifyPropertyChanged接口以使更改受到影响。
&#34; Mark W&#34;已经对此发表了评论。
https://stackoverflow.com/a/1316417/2470362
ObservableCollection类
表示动态数据集合,在添加,删除项目或刷新整个列表时提供通知。
所以当你改变模型属性时它不会通知。
答案 1 :(得分:-1)
我有一个对象的ObservableCollection作为子属性。.即使实现INotifyPropertyChanged >>,它的作用也与您抱怨的一样。
解决了这个问题:
Obj.ObsCol = new ObservableCollection<Obj.ObsCol>