Click here to Skip to main content
15,886,036 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello everyone,

I have a grid with six columns and eight rows, the first row like the first column is reserved for static data(predefined textblocks) the rest of the cells consist of stack panels, each panel has 4 textblocks, I want to bind every stack panel to the database depends on its coordinates so the solutions that came to my mind are:

1- I bind the datacontext property of the stack panel to a converter that takes the stack panel(textblock parent) and extract its grid.row and grid.column ( don't know if I can do it or how ) and use a linq expression to extract the appropriate data and return it as datacontext.

XML
<StackPanel DataContext="{Binding Converter=ConvertToDatacontext}">
                                <TextBlock Text="{Binding Path=FieldOne}" />
                                <TextBlock Text="{Binding Path=FieldTwo}" />
                                <TextBlock Text="{Binding Path=Fieldthree}" />
                                <TextBlock Text="{Binding Path=FieldFour}" />
                            </StackPanel>



and here is ConvertToDatacontext:

C#
public class ConvertToDatacontext: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            StackPanel Sp = value as StackPanel;
            int row = 0;// Get SP row in the grid
            int column = 0;// Get SP Column in the grid
            using (EmpoisDataContext db = new EmpoisDataContext())
            {
                return from stackpanesl in db.Cours
                       where stackpanesl.IDRow == row && stackpanesl.IDColumn == column
                       select stackpanesl;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }


2- Bind the Grid to a Converter and create a class of 42 (6*7) and set the path of each stack panel to the appropriate field number and each text block to the appropriate path.

I really wish you evaluate my solutions and suggest any better one since I am not professional in Wpf and the binding stuff but still trying to be.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900