Workaround for WPF content clutter when dragging the DataGrid scroll bar

Source: Internet
Author: User

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

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.