binding data to an arraylist in xaml

Oct 25, 2008 at 8:46 PM
I am creating a datagrid which work very nicely 
But I have a ComboBoxColumm
which look like this

 

 

<dg:DataGridComboBoxColumn DataFieldBinding="{Binding Path=Opcode}" Header="Op Code" >
    <dg:DataGridComboBoxColumn.ItemsSource>
        <col:ArrayList>
            <sys:String>a</sys:String>
            <sys:String>s</sys:String>
            <sys:String>l</sys:String>
            <sys:String>st</sys:String>
        </col:ArrayList>
    </dg:DataGridComboBoxColumn.ItemsSource>
</dg:DataGridComboBoxColumn>

But the complete data is in a resource so I would like to use it directly.

something like:
<col:ArrayList ItemSource="{Binding Source={StaticResource OpCodes}, XPath=/OpList/Number/OpName}" />

why is <dg:DataGridComboBoxColumn ItemsSource ="{Binding Source={StaticResource OpCodes}, XPath=/OpList/Number/OpName}" />

not working either.

Andre

 

Coordinator
Oct 30, 2008 at 1:55 PM
Could you be a little more specfic on what is not working.  Also, have you tried the v1 DataGrid bits?  Maybe that can help you out as well.
Oct 30, 2008 at 5:50 PM
It all work great but I cannot perform what I believe to be a perfectly legitimate operation.

My problem and sorry if it is not clear.

I would like to bind a StaticResource in a DataGridComboBoxColumn .

A combobox expect an arraylist as an itemsource. I think, but have no proof, that it is the only type of input that a combobox will accept.

So my problem has become how to bind a static resource to an arraylist.
If someone know how to bind a static resource to a combobox this would also solve this problem.

I think I am working with the most recent version of the datagrid.

Hope this clarify my question.

Andre
Coordinator
Oct 31, 2008 at 2:24 PM
A ComboBox.ItemsSource is not bound by ArrayList only.  It can accept any IEnumerable object.  A IEnumberable StaticResource should work just fine.  Here is a small example:

 

 

<ObjectDataProvider x:Key="customerDataProvider" ObjectType="{x:Type local:Customers}" MethodName="GetCustomers" />

 

 

 

<dg:DataGridComboBoxColumn SelectedValueBinding="{Binding CustomerID}" 
            SelectedValuePath="CustomerID" 
            DisplayMemberPath="ContactName"
            Header="CustomerID (ContactName alias using SelectedValueBinding)"
            ItemsSource="{Binding Source={StaticResource customerDataProvider}}">
</dg:DataGridComboBoxColumn>

What excactly is going wrong when you declare a StaticResource?

 

Nov 2, 2008 at 9:55 AM
 I hate to post a long example but here it is.
The dg:DataGridComboBoxColumn.ItemsSource is intialize as an arraylist. 
The ListBox ItemsSource uses a static resource.
It is not possible to use the same static resource to initialize the dg:DataGridComboBoxColumn.ItemsSource
this is my problem. This sould be possible. I want to use one column of my data to init a combobox which is a column.

Thanks you for trying
Andre

 
<Window x:Class="WpfApplication3.Window1"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit" 
    xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <XmlDataProvider x:Key="OpCodes">
            <x:XData>
                <OpList xmlns="">
                    <Number OpNumber="0"><OpName>h</OpName><OpDescription>Halt</OpDescription></Number>
                    <Number OpNumber="1"><OpName>a</OpName><OpDescription>Add</OpDescription></Number>
                    <Number OpNumber="-1"><OpName>s</OpName><OpDescription>Subtract</OpDescription></Number>
                </OpList>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="128*" />
                <RowDefinition Height="134*" />
        </Grid.RowDefinitions>
        <dg:DataGrid>
            <dg:DataGrid.Columns>
            <dg:DataGridTextColumn DataFieldBinding="{Binding Path=Label}" Header="Label " CanUserSort="False" />
            <dg:DataGridComboBoxColumn DataFieldBinding="{Binding Path=Opcode}" Header="Op Code" >
            <dg:DataGridComboBoxColumn.ItemsSource>
                <col:ArrayList>
                    <sys:String>h</sys:String>
                    <sys:String>a</sys:String>
                     <sys:String>s</sys:String>
                </col:ArrayList>
            </dg:DataGridComboBoxColumn.ItemsSource>
            </dg:DataGridComboBoxColumn>
            </dg:DataGrid.Columns>
        </dg:DataGrid>
        <ListBox Grid.Row="1">
            <ListBox ItemsSource="{Binding Source={StaticResource OpCodes}, XPath=/OpList/Number/OpName}" />
        </ListBox>
    </Grid>
</
Window>