DCSIMG
עמוד הבית| חבילות השירות שלנו| חומר חופשי| צור קשר
Siverlight & custom Data Binding - בלוג היועצים של מיקרוסופט ישראל

בלוג היועצים של מיקרוסופט ישראל

Siverlight & custom Data Binding

 

סוגיה מעניינת שנתקל בה לקוח שלי ונראה לי שיכולה לעניין גם אחרים...
(הקרדיט לשגיא קרני על הפתרון...)

נניח שיש לי אפליקצית silverlight 2.0, אשר מקבלת משירות מבנה נתונים שאני מעוניין להציג ב - datagrid.
אני מעוניין לבצע binding ל - datagrid אך מספר העמודות לא ידוע לי מראש.

כמובן שהאוביקט הנוח ביותר להכיל מבנה טבלאי עם מספר עמודות גמיש הוא datatable, ואפילו ניתן לבצע לו binding.
אבל...datatable אינו נתמך ב - silverlight.

אי לכך, פתרון לבעייה ניתן לפתור ע"י שימוש ב - xml:

1. נגדיר מבנה xml שמתאר מבנה טבלאי. לדוגמא:

<table>

                <columns>

                                <column name="col1" title="Column 1"/>

                                <column name="col2" title="Column 2"/>

                <columns>

                <rows>

                                <row>

                                                <col1>12</ col1>

                                                < col2>nice column</ col2>

                                </row>

                                <row>

                                                <col1>13</ col1>

                                                < col2>very nice column</ col2>

                                </row>

                </rows>

<table>

חשוב - במקרה זה הגדרתי 2 עמודות (col1, col2) אך כמובן שניתן להגדיר כל מספר של עמודות. כמו כן, מספר העמודות לא ידוע מראש.

2. האפליקציה מקבלת מהשירות את מבנה הנתונים כמחרוזת ו"טוענת" את הנתונים לאוביקט XDocument, "חוקרת" את העמודות ב - xml ומייצרת ב - datagrid עמודה מתאימה עם הגדרת binding מתאימה (שימו לב לדוגמא):
בהגדרת ה - binding של העמודה אנו משייכים לה רכיב שמבצע את פירוק הנתונים מתוך הרשומה ומייצר עבור העמודה את הערך שלה מתוך נתוני הרשומה.

3. מבצע binding של ה-"rows" ל - datagrid.
חשוב להגדר ב - datagrid: AutoGenerateColumns=false.

4. אם מעוניינים לאפשר עדכון של מבנה הנתונים מתוך ה-datagrid, יש להוסיף מימוש בפונקציה XmlColumnConverter.Convertback

דוגמא:

private void BindTabledDataToDatagrid(DataGrid dataGrid, string data)

{

//parse the data in xml object

XDocument xmlData = XDocument.Parse(data);

 

//create list of columns defenitions:

//loop on the columns defenition from the xml

//and for each column create object instance with the column data

var columns = from col in xmlData.Descendants("column")

select new

{

ColumnName = col.Attribute(XName.Get("name")).Value,

Title = col.Attribute(XName.Get("title")).Value

};

 

//remove all old coluns

dataGrid.Columns.Clear();

 

//the converter instance that know how to get value form the xml data into datagrid column

IValueConverter columnConverter = new XmlColumnConverter();

 

//loop on the columns defenitions list and for each column defenition create data grid column

foreach (var columnDef in columns)

{

DataGridTextColumn column = new DataGridTextColumn();

column.Header = columnDef.Title;

 

//define the binding for this column

column.Binding = new System.Windows.Data.Binding();

 

//the value that will pass to the binding coverter class

column.Binding.ConverterParameter = columnDef.ColumnName;

column.Binding.Converter = columnConverter;

 

//add the column to the datagrid

dataGrid.Columns.Add(column);

}

 

 

dataGrid.AutoGenerateColumns = false;

dataGrid.ItemsSource = xmlData.Descendants("row");

}

 

 

/// <summary>

/// The Binding converter class get XElement row (as value).

/// It handle the column value by geting the element name

/// that represent the column in the "parameter" parameter.

/// </summary>

public class XmlColumnConverter : IValueConverter

{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{

return (this.Convert((XElement)value, (string)parameter));

}

 

private string Convert(XElement data, string columnName)

{

return (data.Element(XName.Get(columnName)).Value);

}

 

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

{

throw new NotImplementedException();

}

}

 

תוכן התגובה

alikl כתב/ה:

טל

אני אהבתי איך שאתה פיצחת את הבעיה

הדבר שהכי אהבתי ששיתפת בסיפור הזה עם אחרים - מכל מלמדי השכלתי :)

תודה!

# February 20, 2009 12:40 PM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 5 and 2 and type the answer here:


Enter the numbers above: