绑定静态ObservableCollection的问题(Problems with binding a static ObservableCollection)

编程入门 行业动态 更新时间:2024-10-28 07:32:18
绑定静态ObservableCollection的问题(Problems with binding a static ObservableCollection)

我有一个类(用于测试目的的很少的代码),它包含一个静态ObservableCollection ,它从别处填充:

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

...和一个带有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>

当我以编程方式尝试绑定时:

list.ItemsSource = Containers.TestClass.TestCollection;

...它工作得很好。 但是,当我尝试通过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>

......什么都不显示。

我也尝试设置DataContext :

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

......并使用路径......

......没有任何东西再次显示出来。

另外,不知道它是否重要,但是当我让我的类static ,我的XAML代码中出现错误:

类型'TestClass'是抽象的,并且必须包含显式值。

所以,这是不可能的。

任何想法如何通过XAML绑定ObservableCollection ?

I have a class (very little code for testing purposes) which contains a static ObservableCollection, which gets populated from elsewhere:

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

... and a basic WPF window with a ListBox:

<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:

<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:

<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:

The type 'TestClass' is abstract and must include an explicit value.

So, that's out of the question.

Any idea how I can bind that ObservableCollection through XAML?

最满意答案

问题是你试图在实例对象上找到静态属性

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

这里dataSource指向TestClass的一个实例,并通过绑定Path到TestCollection,你要求绑定到一个静态的实例属性TestCollection。 这就是为什么它不起作用。

您必须使用x:Static标记扩展来绑定到静态属性。 ( 注意你并没有创建TestClass的任何实例对象

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

还要通知静态属性绑定一次。 如果您在运行时更改了实例中不会反映回UI的实例。 在您的情况下,您正在处理ObservableCollection,因此当您从集合中添加/删除项目时,它将在UI上刷新,但如果您重新初始化列表,则更改将不会反映在UI上。 如果你想更新UI,你必须引发StaticPropertyChangedEvent 。 如果有兴趣,请在这里查看我的答案。

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. That's why it's not working.

You have to use x:Static markup extension to bind to static properties. (Note you are not creating any instance object of 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. 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. In case you want to update UI, you have to raise StaticPropertyChangedEvent. In case interested, check out my answer here.

更多推荐

本文发布于:2023-08-06 11:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   静态   ObservableCollection   static   binding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!