WPF ListBox ItemsSource StaticResource / Bindingの質問
-
10-07-2019 - |
質問
次のコードを指定します:
<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>
この例では、
<ListBox ItemsSource="{StaticResource myCol}" />
&quot; CollectionViewSource&quot;にバインドできないというエラーが表示されます。オブジェクト。
しかし、他のリストボックス:
<ListBox ItemsSource="{Binding Source={StaticResource myCol}}" />
完全にバインドします。
それで私の質問は、なぜ一方が機能し、もう一方が機能しないのかということです。最後に、両方のItenSourceが同じ&quot; CollectionViewSource&quot;に設定されていません。オブジェクト?
ありがとう。
解決
ItemsSourceプロパティのタイプはIEnumerableです。 CollectionViewSourceはIEnumerableではありません。 CollectionViewSourceのViewプロパティは、IEnumerableを提供します。
CollectionViewSourceにバインドすると、BindingはViewプロパティを取得して実際にバインドするのに十分スマートです。 CollectionViewSourceには[DefaultBindingProperty]があるかもしれません。
要約すると、Bindingを実行すると、実際にはCollectionViewSourceではなく、そのViewプロパティにバインドされるという事実になります。
所属していません StackOverflow