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.
<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:
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;
int column = 0;
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.