c# - Xaml 中的 WPF 绑定(bind)

标签 c# wpf xaml

类似的问题很多,我似乎无法弄清楚发生了什么。

我正在努力填充我的 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。您需要实现 INotifyPropertyChangeMain 上类和更改报告以发出这样的属性更改信号

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/

相关文章:

c# - WPF 新手,如何在我的表单中创建这些彩色条?

xaml - 设置 x :DeferLoadStrategy in code behind

c# - 可缓存的认证响应

c# - 如何从 C# Web API REST 服务器缓存缓慢的资源初始化?

c# - 如何使用 webkit 浏览器引擎构建 web 浏览器 for windows

c# - 在异步操作后立即调用 Task.Wait() 是否等同于同步运行相同的操作?

c# - WCF 客户端对象反序列化通知

c# - 在 Prism 6 中加载所有模块后使用 ViewModel 初始化 Shell

wpf - 如何为具有现有控件模板的 XAML 按钮设置全局样式

C# WPF - 从任何类写入 Listview