手机
当前位置:查字典教程网 >编程开发 >C#教程 >WPF拖动DataGrid滚动条时内容混乱的解决方法
WPF拖动DataGrid滚动条时内容混乱的解决方法
摘要:在WPF中,如果DataGrid里使用了模板列,当拖动滚动条时,往往会出现列表内容显示混乱的情况。解决方法就是在Binding的时候给Upd...

在WPF中,如果DataGrid里使用了模板列,当拖动滚动条时,往往会出现列表内容显示混乱的情况。解决方法就是在Binding的时候给UpdateSourceTrigger赋值。

<Grid> <Grid.RowDefinitions> <RowDefinition Height="25"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Button Height="23" 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> <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="80"></DataGridTextColumn> <DataGridTemplateColumn Header="Age" Width="70"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Margin="5" Text="{Binding Age, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Header="Course" Width="100"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox Margin="5" ItemsSource="{Binding CourseSource}" Text="{Binding Course, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></ComboBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid>

后台代码如下:

public class Student { public string Name { get; set; } public string Age { get; set; } public List<string> CourseSource { get; set; } = new List<string>() { "C", "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 <= 50; i++) { var student = new Student() { Name = $"student{i}" }; students.Add(student); } this.dgStudent.ItemsSource = null; this.dgStudent.ItemsSource = students; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持查字典教程网。

【WPF拖动DataGrid滚动条时内容混乱的解决方法】相关文章:

使用checked语句防止数据溢出的解决方法

解析StreamReader与文件乱码问题的解决方法

程序中两个Double类型相加出现误差的解决办法

.NET创建、删除、复制文件夹及其子文件的实例方法

C# javascript 读写Cookie的方法

c# asp .net 动态创建sql数据库表的方法

c#之利用API函数实现动画窗体的方法详解

c#在控制台输出彩色文字的方法

BarCode条形码基于C# GDI+ 的实现方法详解

新手学习.net的一列好走的路径及方法

精品推荐
分类导航