鉴于以下xaml代码,我想在C#中创建这个xaml代码GridView部分,以便将数据源附加到ListView ItemItemsSource EditBox只是一个文本框类
<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>
我在另一个项目中做了类似的事情。
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.
答案 0 :(得分:0)
我不确定你遇到什么问题,但我怀疑它与DataTemplates有关。我不知道你的EditBox是什么,所以我只使用了TextBox。
var gridView = new GridView { AllowsColumnReorder = true, ColumnHeaderToolTip = "Employee Information" };
// column 1
var col1 = new GridViewColumn { DisplayMemberBinding = new Binding("FirstName"), Header="First Name", Width=100 };
gridView.Columns.Add(col1);
// column 2
var col2 = new GridViewColumn { Header="Last Name", Width=100 };
var fef2 = new FrameworkElementFactory(typeof(TextBox));
fef2.SetValue(TextBox.HeightProperty, 25d);
fef2.SetBinding(TextBox.TextProperty, new Binding("LastName"));
col2.CellTemplate = new DataTemplate { VisualTree = fef2 };
gridView.Columns.Add(col2);
// column 3
var col3 = new GridViewColumn { Header = "ID", Width = 75 };
var fef3 = new FrameworkElementFactory(typeof(TextBox));
fef3.SetValue(TextBox.HeightProperty, 25d);
fef3.SetBinding(TextBox.TextProperty, new Binding("EmployeeNumber"));
col3.CellTemplate = new DataTemplate { VisualTree = fef3 };
gridView.Columns.Add(col3);