WPF ListBox ItemsSource StaticResource / Binding问题

时间:2022-07-21 17:02:56

Given the following code:

给出以下代码:

<Window x:Class="WpfApplication76.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <CollectionViewSource x:Key="myCol">
            <CollectionViewSource.Source>
                <col:ArrayList>
                    <ListBoxItem>Uno</ListBoxItem>
                    <ListBoxItem>Dos</ListBoxItem>
                    <ListBoxItem>Tres</ListBoxItem>
                </col:ArrayList>
            </CollectionViewSource.Source>
        </CollectionViewSource>
    </Window.Resources>

    <Grid>
        <ListBox ItemsSource="{StaticResource myCol}" />
        <ListBox ItemsSource="{Binding Source={StaticResource myCol}}" />
    </Grid>

</Window>

In this example, the

在这个例子中,

<ListBox ItemsSource="{StaticResource myCol}" />

Gives me an error complaining that it cannot bind to a "CollectionViewSource" object.

给我一个错误,抱怨它无法绑定到“CollectionViewSource”对象。

But the other listbox:

但另一个列表框:

<ListBox ItemsSource="{Binding Source={StaticResource myCol}}" />

binds perfectly fine.

结合完美。

So my question is why does one work and the other one does not? AT the end, aren't both ItenSources being set to the same "CollectionViewSource" object?

所以我的问题是为什么一个工作而另一个不工作?最后,是不是两个ItenSources都设置为相同的“CollectionViewSource”对象?

Thank you.

谢谢。

1 个解决方案

#1


5  

The ItemsSource property is of type IEnumerable. A CollectionViewSource is not an IEnumerable. CollectionViewSource's View property will give you an IEnumerable.

ItemsSource属性的类型为IEnumerable。 CollectionViewSource不是IEnumerable。 CollectionViewSource的View属性将为您提供IEnumerable。

When you Bind to a CollectionViewSource the Binding is smart enough to grab the View property and actually bind to that. Maybe CollectionViewSource has a [DefaultBindingProperty] on it.

绑定到CollectionViewSource时,Binding非常智能,可以抓取View属性并实际绑定到该属性。也许CollectionViewSource上有一个[DefaultBindingProperty]。

It boils down to the fact that when you go through the Binding you don't actually bind to the CollectionViewSource, but its View property.

它归结为这样一个事实:当你完成Binding时,你实际上并没有绑定到CollectionViewSource,而是它的View属性。

#1


5  

The ItemsSource property is of type IEnumerable. A CollectionViewSource is not an IEnumerable. CollectionViewSource's View property will give you an IEnumerable.

ItemsSource属性的类型为IEnumerable。 CollectionViewSource不是IEnumerable。 CollectionViewSource的View属性将为您提供IEnumerable。

When you Bind to a CollectionViewSource the Binding is smart enough to grab the View property and actually bind to that. Maybe CollectionViewSource has a [DefaultBindingProperty] on it.

绑定到CollectionViewSource时,Binding非常智能,可以抓取View属性并实际绑定到该属性。也许CollectionViewSource上有一个[DefaultBindingProperty]。

It boils down to the fact that when you go through the Binding you don't actually bind to the CollectionViewSource, but its View property.

它归结为这样一个事实:当你完成Binding时,你实际上并没有绑定到CollectionViewSource,而是它的View属性。