Binding Indexed Property

Sep 10, 2008 at 6:49 PM
Edited Sep 10, 2008 at 6:51 PM
I have a class and one of its properties its a list of int values something like this:


  public class EntryInfo
    {
        private byte[] entryImage;
        private long idEntry;
        private long idCategory;
        private List<int> values=new List<int>(5);

        public EntryInfo()
        {
            for (int i = 0; i < 5; i++)
            {
                values.Add(i);
            }
        }

        public List<int> Values
        {
            get
            {
                return values;
            }
        }

//all other properties
}

I'm trying this:

Grid>
            <dg:DataGrid Name="entryInfoDataGrid" VerticalAlignment="Top"  ItemsSource="{Binding Source={StaticResource cvs}}"
                         AutoGenerateColumns="False" >
                <dg:DataGrid.Columns>
                    <dg:DataGridTemplateColumn Header=" CountryName">
                        <dg:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Image Source="{Binding Path=EntryImage}" Height="110" Width="150"/>
                            </DataTemplate>
                        </dg:DataGridTemplateColumn.CellTemplate>
                    </dg:DataGridTemplateColumn >
                    <dg:DataGridTextColumn DataFieldBinding="{Binding Path=Values(x)}" />
                </dg:DataGrid.Columns>
            </dg:DataGrid>
    </Grid>


What i want to do is: show each ot the items in the list in  a diferent cell.


How do I specify the index for the item?

Is it that the correct way?


Coordinator
Sep 10, 2008 at 11:41 PM
Does something like this work for you: Values[0], Values[1], etc.
Sep 11, 2008 at 7:12 PM
I half-solve it with a Converter. this way:

        <DataTemplate x:Key="entryInfoCellDataTemplate">
            <Grid>
                <TextBlock HorizontalAlignment="Center" FontSize="20">
                    <TextBlock.Text >
                        <MultiBinding Converter="{StaticResource conv}">
                            <Binding Path="IDEntry"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>  
            </Grid>
        </DataTemplate>       

then from the converter I call next:

public static string GetItemWithCategory(Int64 idEntry)
        {
            string result="";
            var query = from c in entriesInfo where c.IDEntry == idEntry select new { c.Values,c.ActualIndex,c.Index };
            foreach (var row in query)
            {
                if (row.Values.Count > 0 && entriesInfo[row.Index].ActualIndex < entriesInfo[row.Index].Values.Count-1)
                {
                    entriesInfo[row.Index].ActualIndex++;
                    result = row.Values[row.ActualIndex].StrValue; 
                }
            }
            return result;
        }

This way ( I dont know if its the better way) I can show each element in a diferent cell.

But now I have 2 questions:

1.-  How do I bind the currentcell column to send it as parameter to converter. I have the DataTemplate as a Window.Resource so I'm doing that:

<TextBlock HorizontalAlignment="Center" FontSize="20">
                    <TextBlock.Text >
                        <MultiBinding Converter="{StaticResource conv}">
                            <Binding Path="IDEntry"/>
                            <Binding ElementName="entryInfoDataGrid" Path="CurrentCell.Column"/> /// The second value in the values array in the converter says: DependencyProperty: UnsetValue
                                                                                                                                               ///I dont know if its because of the NameScope

                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>

2.- if I use Values[0], Values[1] from where do I get the Index values???
Sep 16, 2008 at 6:44 PM
You probably need to dynamically create the columns for your indexed values like this:

 

for (int j = 0; j < Values.Count; j++)
{
    DataGridTextColumn column2 = new DataGridTextColumn();
    column2.Width = 30;
    column2.Header =
"Value";
    column2.DataFieldBinding =
new Binding(string.Format("Values[{0}]", j));
    msDataGrid.Columns.Add(column2);
}

 

Sep 16, 2008 at 6:54 PM
My only problem with my solution above is, even if it works nicely, it does not work with the DataGridTemplateColumn because it lacks of the DataFieldBinding property.
So if you want to use the DataGridTemplateColumn you will have to create a Template for each of the indexes of your array ( that sucks!).

Grids like Xceed support a property (FieldName) that exists regardless if the column uses templates or not.

Can you guys keep the DataFieldBinding for the DataGridTemplateColumn ?

Thanks!