Skip to main content

Microsoft Silverlight

Answered Question Binding a Listbox ItemsSource to a StaticResource list of objectsRSS Feed

(0)

esite
esite

Participant

Participant

1452 points

311 Posts

Binding a Listbox ItemsSource to a StaticResource list of objects

Hi,

I have a ListBox in the DataTemplate of an AgDataGrid Column. I want to be able to bind the ItemsSource to another collection of objects but cannot seem to get this working.

I am battling to get a handle on this StaticResource concept but from reading this http://msdn.microsoft.com/en-us/library/cc189045(VS.95).aspx I understand that I can define a Resource and then let the ListBox point to that.

I have not been able to make this work though, the below example just raise an exception after a while. 

In the constructor I have this line of code:

this.Resources.Add("DocumentTypes", DocumentTypes); //DocumentTypes is a List of DocumentType with a DocumentTypeKey field

And the below XAML for the Grid

<grid:AgDataGrid x:Name="grid" Margin="8,0,8,8" >
              <grid:AgDataGrid.Columns>
                   <grid:AgDataGridColumn FieldName="FileName" Width="248"/>
                <grid:AgDataGridColumn FieldName="DocumentTypeKey" Width="108">
                    <grid:AgDataGridColumn.DisplayTemplate>
                        <ControlTemplate TargetType="ListBox">
                            <ListBox x:Name="lbDocumentTypeKey" ItemsSource="{Binding Path=DocumentTypeKey, Source={StaticResource DocumentTypes}}" Height="25"/>
                        </ControlTemplate>
                    </grid:AgDataGridColumn.DisplayTemplate>
                </grid:AgDataGridColumn>
            </grid:AgDataGrid.Columns>
         </grid:AgDataGrid>

Thanks in advance,

Anton

Please mark replies as answers if they answered your question.

Anton Swanevelder
eSite Solutions

mchlSync
mchlSync

Star

Star

14606 points

2,730 Posts

Silverlight MVP
Answered Question

Re: Binding a Listbox ItemsSource to a StaticResource list of objects

I think you can use ElementName Binding if you are using Silverlight 3.

For example:

<ListBox x:Name="lbDocumentTypeKey" ItemsSource="{Binding ElementName=OtherControlName, Path=DataContext.AnotherCollection}" Height="25"/>

OtherControlName = is the name of other control that is outside of AgDataGrid.

 

 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Binding a Listbox ItemsSource to a StaticResource list of objects

If you are using Silverlight 2, you can probably take a look at CompositeWPF Version 2 sample:

THe path of Example: RI\Silverlight\StockTraderRI.Modules.Watch.Silverlight\WatchList\WatchListView.xaml.cs

Just read the code to know how to workaround the ElementName Binding in Prism.

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


esite
esite

Participant

Participant

1452 points

311 Posts

Re: Re: Binding a Listbox ItemsSource to a StaticResource list of objects

Thanks for your help Michael,

I am using SL3. Never made the connection to just use another control and bind to that.

I would still love to know if this can be achieved using the Page Resources and the StaticResource markup.

But for now this will do and thanks again.

Please mark replies as answers if they answered your question.

Anton Swanevelder
eSite Solutions

esite
esite

Participant

Participant

1452 points

311 Posts

Re: Re: Binding a Listbox ItemsSource to a StaticResource list of objects

The guys from DevExpress created a awesome little Sample application that does exactly this. http://www.devexpress.com/Support/Center/p/Q211475.aspx

Please mark replies as answers if they answered your question.

Anton Swanevelder
eSite Solutions
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities