类似的问题很多,我似乎无法弄清楚发生了什么。
我正在努力填充我的 ListView,绑定(bind)到一个 ObservableCollection...
代码:
<controls:MetroWindow
...blah...
Title="MainWindow" x:Name="Main" DataContext="{Binding ElementName=Main}"
<ListBox Grid.Row="0" x:Name="FileNames" HorizontalAlignment="Left" Height="221" Margin="10,62,0,0" VerticalAlignment="Top" Width="258"
SelectionChanged="FileNames_SelectionChanged" BorderThickness="2"
ItemsSource="{Binding Reports, UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label CacheMode="{Binding Path=FileName,UpdateSourceTrigger=PropertyChanged}"></Label>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
//Code Behind
public ObservableCollection<ReportsModel> Reports { get; set; }
Reports = setReports();
ReportsModel impls INotifyPropertyChanged
public sealed class ReportsModel : INotifyPropertyChanged
我可以介入并看到 Reports 得到了很好的填充,文件名字段也被填充在每个报告上,但我的 ListBox 中没有显示任何内容。
如有任何帮助,我们将不胜感激。
最佳答案
持有 ObservableCollection<ReportsModel>
的引用名为 Reports
不能有一个普通的 getter/setter。您需要实现 INotifyPropertyChange
在 Main
上类和更改报告以发出这样的属性更改信号
private ObservableCollection<ReportsModel> _reports;
public ObservableCollection<ReportsModel> Reports
{
get { return _reports; }
set
{
_reports = value;
PropertyChanged("Reports");
}
}
ReportsModel impls INotifyPropertyChanged
持有的数据与馆藏的属性(property)无关Reports
绑定(bind)更改通知。
关于c# - Xaml 中的 WPF 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31733670/