2

I got following CollectionViewSource definition in XAML:

 <UserControl.Resources>
        <CollectionViewSource x:Key="PersonsViewSource" Source="{Binding AvailablePersons}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Surname" Direction="Ascending" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </UserControl.Resources>

I got a combobox bound to this CVS and would like to access the View from code like this:

(Resources["PersonsViewSource"] as CollectionViewSource).View

However even if I try to access it in constructor after InitializeComponent() or in Loaded event handler View & Source are still null.

When application is shown to me in browser though binding has already taken place and if I now put a breakpoint somewhere view & source are not null now.

So when exactly is Source set? Where can I access the view on the stage of loading my application?

Maybe this is a general binding question not really regarding viewsource, when is static resource binding set?

Valentin Kuzub
  • 11,703
  • 7
  • 56
  • 93

1 Answers1

1

The binding is set during Initialise, as this line is parsed:

<CollectionViewSource x:Key="PersonsViewSource" Source="{Binding AvailablePersons}">

but that binding points to whatever data exists in the AvailablePersons property at that time. It will remain empty until any related asynchronous load completes.

With Silverlight, you are generally interested in data loaded events, rather than when the visual tree goes on display.

Can you tell us more about how you are fetching the data for AvailablePersons?

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • sure, available persons arent fetched (this is a sample name) actually this value (ObservableCollection) is set in viewmodel constructor and contains like 10 items which are available without any data load or service calls. http://stackoverflow.com/questions/6305608/how-to-preserve-twoway-binding-of-currentitem-when-databinding-to-collectionviews here is full code & another question too. – Valentin Kuzub Jun 11 '11 at 10:18