Creating ListBox ItemTemplate RRS feed

  • Question

  • When I create a new ItemTemplate/DataTemplate in Blend 3 for a ListBox, the XAML creates the following.

    <StackPanel Orientation="Horizontal" DataContext="{Binding Source={StaticResource STEP_RESULTDataSource}}">

    In my .CS file, I query some data from SQL source and use lstBox.ItemsSource = results.

    I have to delete DataContext="{Binding Source={StaticResource STEP_RESULTDataSource}}" in order to bind and display the data when running my app.  Is there a way to disable this from being added in XAML.


    Thursday, April 2, 2009 12:51 PM

All replies

  • Can you expose a property on the object that is set at the DataContext of the root panel and set that property? Alternatively, can you set the local DataContext of the ListBox to your object that you create in code, and set the ItemsSource just as you would otherwise?

    The fact that we set the DataContext at the root is by design - this enables users to easily switch out the DataContext of the entire document with something else (this is something that allows us to provide the sample data experience), and a lot of people prefer this approach as opposed to Source bindings being all over the document.

    Saturday, April 4, 2009 9:01 PM