WPF dragging the DataGrid scroll bar solution for content Confusion _c# tutorial

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.