Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Binding a Listbox ItemsSource to a StaticResource list of objects
4 replies. Latest Post by esite on July 3, 2009.
(0)
esite
Participant
1452 points
311 Posts
07-02-2009 2:25 PM |
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
mchlSync
Star
14606 points
2,730 Posts
07-02-2009 2:37 PM |
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.
mchlsync
07-02-2009 2:43 PM |
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.
07-02-2009 3:56 PM |
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.
07-03-2009 5:34 AM |
The guys from DevExpress created a awesome little Sample application that does exactly this. http://www.devexpress.com/Support/Center/p/Q211475.aspx