【问题标题】:WPF ListView not showing contents of Observable CollectionWPF ListView 未显示可观察集合的内容
【发布时间】:2011-08-22 09:48:17
【问题描述】:

我在 WPF 应用程序中有一个列表视图,它应该显示可观察集合的内容。我曾在较旧的版本中显示它,但在尝试更频繁地更新列表视图时,我对 XAML 进行了相当多的更改,在将其放回原处后,它现在不再显示任何内容。

这是 XAML:

<TabItem GotFocus="TabSelect">
    <TabItem.Header>
        <TextBlock FontSize="12pt">error log</TextBlock>
    </TabItem.Header>
    <Grid><ListView Name="JfifoList" ItemsSource="{Binding JFifoErrorCollection}" Background="Transparent">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Time}" Header="time" Width="100" />
                    <GridViewColumn DisplayMemberBinding="{Binding FEStatus}" Header="fe status" Width="100" />
                    <GridViewColumn DisplayMemberBinding="{Binding BEStatus}" Header="be status" Width="100" />
                    <GridViewColumn DisplayMemberBinding="{Binding Trigger}" Header="trigger" Width="100" />
                </GridView>
            </ListView.View>
        </ListView></Grid>
</TabItem>

我确实将列表的 DataContext 设置为“this”窗口:

JfifoList.DataContext = this;

在调试时,我可以看到集合已填充,因此有数据要显示......虽然在调试时,我反复看到一个错误,上面写着“在 PresentationFramework 中发生'System.NotSupportedException' 类型的第一次机会异常” .dll”在调试输出中。

编辑:我环顾四周,发现 UI 元素必须在 UI 线程中更新......由于某种原因(我没有在代码中创建任何),我的程序中有许多线程在运行,他们似乎都在更新我的 ObservableCollection...不知道这是否是问题...

【问题讨论】:

  • 1.您是否在 ItemsSource="{Binding JFifoErrorCollection}" 的绑定失败中看到任何绑定错误? 2. 用ListBox 替换你的ListView 并通过设置DiaplayMemberPath 来显示JFifoErrorCollection 的一些成员。如果列表框显示数据,则您的 ListView 配置存在问题。 3. 即使你的 observable 集合被填充到另一个线程中,你是否在 viewmodel 模块的那个线程中提出了NotifyPropertyChanged("JFifoErrorCollection")
  • Listbox 也没有显示任何内容... Observable Collection 本身不会引发该事件吗?
  • 遗憾的是,实际上可观察的集合并不是线程安全的。 :(
  • 无论如何我都不想要多个线程......它们似乎就在那里,而我没有要求它们。我怎样才能只在一个线程上运行?

标签: c# wpf xaml data-binding listview


【解决方案1】:

对绑定到视图的列表内容的修改必须在 UI 线程中完成。 要捕获您的 App UI 线程,您可以使用以下内容:

Application.Current.Dispatcher.Invoke(DispatcherPriority.Render, new Action(() => JfifoList = whateverData));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2016-11-08
    相关资源
    最近更新 更多