简体   繁体   English

绑定静态ObservableCollection的问题

[英]Problems with binding a static ObservableCollection

I have a class (very little code for testing purposes) which contains a static ObservableCollection , which gets populated from elsewhere: 我有一个类(用于测试目的的代码很少),它包含一个静态的ObservableCollection ,它从其他地方填充:

public class TestClass
{
    public static ObservableCollection<int> TestCollection = new ObservableCollection<int>();
}

... and a basic WPF window with a ListBox : ...和一个带有ListBox的基本WPF窗口:

<Window x:Class="app.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox x:Name="list"/>
    </Grid>
</Window>

When I tried binding programmatically: 当我尝试以编程方式绑定时:

list.ItemsSource = Containers.TestClass.TestCollection;

... it worked just fine. ......工作得很好。 However, when I try to perform binding through XAML: 但是,当我尝试通过XAML执行绑定时:

<Window x:Class="app.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="clr-namespace:app.Containers"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <d:TestClass x:Key="dataSource"/>
    </Window.Resources>
    <Grid>
        <ListBox x:Name="list" ItemsSource="{Binding Source={StaticResource dataSource}, Path=TestCollection}"/>
    </Grid>
</Window>

... nothing gets displayed. ......什么都没有显示出来

I've also tried setting DataContext : 我也尝试过设置DataContext

<Window.Resources>
    <l:LifeEngine x:Key="dataSource"/>
</Window.Resources>
<Window.DataContext>
    <Binding Source="{StaticResource dataSource}"/>
</Window.DataContext>

...and using path... ......并使用路径......

...and nothing gets displayed once again. ......没有任何东西再次显示出来。

Also, not sure if it matters, but when I make my class static I get an error in my XAML code saying: 此外,不确定它是否重要,但是当我使我的类static ,我的XAML代码中出现错误:

The type 'TestClass' is abstract and must include an explicit value. 类型'TestClass'是抽象的,必须包含显式值。

So, that's out of the question. 所以,这是不可能的。

Any idea how I can bind that ObservableCollection through XAML? 知道如何通过XAML绑定ObservableCollection吗?

Problem is you are trying to find static property on an instance object . 问题是您正在尝试在实例对象上查找静态属性

<ListBox ItemsSource="{Binding Source={StaticResource dataSource},
                               Path=TestCollection}"/>

Here dataSource is pointing to an instance of TestClass and by binding Path to TestCollection, you are asking to bind to an instance property TestCollection which is static instead. 这里dataSource指向TestClass的一个实例,并通过绑定Path到TestCollection,你要求绑定到一个静态的实例属性TestCollection。 That's why it's not working. 这就是为什么它不起作用。

You have to use x:Static markup extension to bind to static properties. 您必须使用x:Static标记扩展来绑定到静态属性。 ( Note you are not creating any instance object of TestClass ) 注意,您没有创建TestClass的任何实例对象

<ListBox ItemsSource="{Binding Source={x:Static d:TestClass.TestCollection}}"/>

Also be informed that static properties binds one time. 还要注意静态属性绑定一次。 If you change the instance at runtime that won't be reflected back on UI. 如果在运行时更改实例,则不会反映在UI上。 In your case you are dealing with ObservableCollection so when you add/delete item from collection it will be refreshed on UI but in case you reinitialize the list that change won't be reflected back on UI. 在您的情况下,您正在处理ObservableCollection,因此当您从集合中添加/删除项目时,它将在UI上刷新,但如果您重新初始化列表,则更改将不会反映在UI上。 In case you want to update UI, you have to raise StaticPropertyChangedEvent . 如果要更新UI,则必须引发StaticPropertyChangedEvent In case interested, check out my answer here . 如果有兴趣,请在这里查看我的答案。

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

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