WPF拖动DataGrid滚动条时内容混乱的解决方法

 更新时间:2016年10月12日 10:31:44   作者:sunny906  
这篇文章主要介绍了WPF拖动DataGrid滚动条时内容混乱的解决方法

在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;
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C#操作PowerPoint的方法

    C#操作PowerPoint的方法

    这篇文章主要介绍了C#操作PowerPoint的方法,涉及C#针对PowerPoint的打开、读取、播放等技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • 递归案例分享

    递归案例分享

    一般定义:程序调用自身的编程技巧称为递归( recursion)。
    2014-05-05
  • 详解C#中Dictionary<TKey,TValue>的存储结构

    详解C#中Dictionary<TKey,TValue>的存储结构

    无论是实际的项目中,还是在我们学习的过程中,都会重点的应用到Dictionary<TKey, TValue>这个存储类型,所以本文就来为大家介绍一下这一存储结构的相关知识,希望对大家有所帮助
    2023-11-11
  • 使用Spire.Barcode程序库生成二维码的实例解析

    使用Spire.Barcode程序库生成二维码的实例解析

    这篇文章主要介绍了使用Spire.Barcode程序库生成二维码的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-12-12
  • C#中Response.Write常见问题汇总

    C#中Response.Write常见问题汇总

    这篇文章主要介绍了C#中Response.Write常见问题汇总,总结了C#中Response.Write的常用技巧,非常实用,需要的朋友可以参考下
    2014-09-09
  • 使用C#实现RTP数据包传输 参照RFC3550

    使用C#实现RTP数据包传输 参照RFC3550

    本篇文章小编为大家介绍,使用C#实现RTP数据包传输 参照RFC3550,需要的朋友参考下
    2013-04-04
  • C# 代码大小写规范说明

    C# 代码大小写规范说明

    这篇文章主要介绍了C# 代码大小写规范说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • C# 拷贝数组的几种方法(总结)

    C# 拷贝数组的几种方法(总结)

    下面小编就为大家带来一篇C# 拷贝数组的几种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • C# 基于TCP 实现扫描指定ip端口的方式示例

    C# 基于TCP 实现扫描指定ip端口的方式示例

    本文主要介绍了C#基于TCP实现扫描指定ip端口的方式示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • c# 定期重启程序操作的实现

    c# 定期重启程序操作的实现

    本文主要介绍了c# 定期重启程序操作的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-09-09

最新评论