Workaround for WPF content clutter when dragging the DataGrid scroll bar
In WPF, if a template column is used in a DataGrid, when you drag the scroll bar, the list content appears cluttered. The solution is to assign a value to UpdateSourceTrigger at the time of binding.
<Grid> <Grid. RowDefinitions> <RowDefinitionHeight= "+"></RowDefinition> <RowDefinition></RowDefinition> </grid.rowdefinitions> <ButtonHeight= "All"Click= "Button_Click"Content= "click"Grid.Row= "0"></Button> <DataGridName= "Dgstudent"AutoGenerateColumns= "False"isenabled= "True"Grid.Row= "1"enablecolumnvirtualization= "True"enablerowvirtualization= "True"> <DataGrid. Columns> <DatagridtextcolumnHeader= "Name"Binding= "{Binding Name}"Width= "a"></Datagridtextcolumn> <DatagridtemplatecolumnHeader= "Age"Width= "The "> <Datagridtemplatecolumn. CellTemplate> <DataTemplate> <TextBoxMargin= "5"Text= "{Binding age, Mode=twoway, updatesourcetrigger=propertychanged}"></TextBox> </DataTemplate> </datagridtemplatecolumn.celltemplate> </Datagridtemplatecolumn> <DatagridtemplatecolumnHeader= "Course"Width= "+"> <Datagridtemplatecolumn. CellTemplate> <DataTemplate> <ComboBoxMargin= "5"ItemsSource= "{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 classStudent { Public stringName {Get;Set; } Public stringAge {Get;Set; } Publiclist<string> Coursesource {Get;Set; } =Newlist<string> () {"C","C + +","C #" }; Public stringCourse {Get;Set; } } Private voidButton_Click (Objectsender, RoutedEventArgs e) { varStudents =NewList<student>(); for(inti =1; I <= -; i++) { varStudent =NewStudent () {Name= $"Student{i}" }; Students. ADD (student); } This. Dgstudent.itemssource =NULL; This. Dgstudent.itemssource =students; }
Workaround for WPF content clutter when dragging the DataGrid scroll bar