In WPF, if a template column is used in a DataGrid, the list content appears cluttered when you drag the scroll bar. The solution is to assign values to UpdateSourceTrigger when binding.
<Grid> <Grid.RowDefinitions> <rowdefinition height= "a" ></RowDefinition> <rowdefiniti on></rowdefinition> </Grid.RowDefinitions> <button height= "click=" Button_Click "content=" Click "grid.row=" 0 "></Button> <datagrid name=" dgstudent "autogeneratecolumns=" False "isenabled=" True " grid.row= "1" enablecolumnvirtualization= "true" enablerowvirtualization= "true" > <DataGrid.Columns> ;D atagridtextcolumn header= "Name" binding= "{Binding name}" width= "></DataGridTextColumn> < Datagridtemplatecolumn header= "Age" width= "> <DataGridTemplateColumn.CellTemplate> <datatemplate" > <textbox margin= "5" text= "{Binding age, Mode=twoway, updatesourcetrigger=propertychanged}" ></textbox&
Gt </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <datagr Idtemplatecolumn header= "Course" width= "> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <combobox margin= "5" Itemss Ource= ' {Binding coursesource} ' text= ' {Binding Course, Mode=twoway, updatesourcetrigger=propertychanged} ' ></ combobox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </datagridtemplatecolumn > </DataGrid.Columns> </DataGrid> </Grid>
The background code is as follows:
public class Student
{public
string Name {get; set;}
public string Age {get; set;}
Public list<string> Coursesource {get; set;} = new list<string> () {"C", "+ +", "C #"};
public string Course {get; set;}
}
private void Button_Click (object sender, RoutedEventArgs e)
{
var students = new list<student> ();
for (int i = 1; I <= i++)
{
var student = new Student ()
{
Name = $ ' student{i} '
};
Students. ADD (student);
}
This.dgStudent.ItemsSource = null;
This.dgStudent.ItemsSource = students;
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.