Given the following xaml code, I would like to create this xaml code the GridView part in C# in order to attach the datasource to the ListView ItemItemsSource
EditBox is just a textbox class
<ListView.View>
<!-- Here is the part i'd like to do with C# -->
<GridView AllowsColumnReorder="true"
ColumnHeaderToolTip="Employee Information">
<GridViewColumn DisplayMemberBinding=
"{Binding Path=FirstName}"
Header="First Name" Width="100"/>
<GridViewColumn Header="Last Name" Width="100" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<l:EditBox Height="25" Value="{Binding Path=LastName}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="ID" Width="75" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<l:EditBox Height="25" Value="{Binding Path=EmployeeNumber}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
i did something similar in another project.
ListView ctrl = (ListView)GetCtrl((string)strctrl);//Rebuild the gridview
ctrl.View = null;
GridView grid = new System.Windows.Controls.GridView();
int c = 0;
foreach (DataColumn lv_col in data.Table.Columns)
{
//Skip columns ending with "_ID"
int ilen = lv_col.ColumnName.Length;
if (lv_col.ColumnName.Substring(ilen - 3) != "_ID" && lv_col.ColumnName != "Deleted")
{
GridViewColumn col = new System.Windows.Controls.GridViewColumn();
if (labels != null && labels.Contains(lv_col.ColumnName))
lv_col.Caption = labels[lv_col.ColumnName].ToString();
col.Header = lv_col.Caption;
Binding colbind = new Binding("[" + c.ToString() + "]");
if (lv_col.DataType.GetType() == typeof(System.DateTime))
{
colbind.StringFormat = date_format;
if (Global.Lng == "F") colbind.ConverterCulture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR"); // #Even on a engrish system, this should display french dates
}
else if (lv_col.DataType.GetType() == typeof(System.Double))
colbind.StringFormat = "0";
col.DisplayMemberBinding = colbind;
grid.Columns.Add(col);
}
c += 1;
}
//Restore column widths
foreach (int i in Enumerable.Range(0, col_widths.Count))
if (i < grid.Columns.Count) grid.Columns[i].Width = col_widths[i];
ctrl.View = grid;
ctrl.ItemsSource = data;
ctrl.UpdateLayout();//This will force the listview to finish displaying.
I’m not sure what part you are having trouble with but I suspect it’s with the DataTemplates. I don’t know what your EditBox is so I just used a TextBox.