国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

WPF拖動DataGrid滾動條時內容混亂的解決方法

2019-10-29 21:20:32
字體:
來源:轉載
供稿:網友

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 靖西县| 瑞丽市| 宕昌县| 综艺| 淅川县| 霍林郭勒市| 正阳县| 福贡县| 托克逊县| 读书| 祥云县| 兴宁市| 松滋市| 衢州市| 邵阳市| 安丘市| 和林格尔县| 县级市| 宁远县| 湾仔区| 胶南市| 合阳县| 黑龙江省| 渝中区| 若羌县| 新沂市| 漯河市| 普定县| 于田县| 甘洛县| 台安县| 武义县| 鸡泽县| 洪洞县| 江山市| 延安市| 新晃| 且末县| 涪陵区| 西充县| 涪陵区|