WPF拖動DataGrid滾動條時內(nèi)容混亂的解決方法
更新時間:2016年10月12日 10:31:44 作者:sunny906
這篇文章主要介紹了WPF拖動DataGrid滾動條時內(nèi)容混亂的解決方法
在WPF中,如果DataGrid里使用了模板列,當拖動滾動條時,往往會出現(xiàn)列表內(nèi)容顯示混亂的情況。解決方法就是在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; }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解C#中Dictionary<TKey,TValue>的存儲結構
無論是實際的項目中,還是在我們學習的過程中,都會重點的應用到Dictionary<TKey,?TValue>這個存儲類型,所以本文就來為大家介紹一下這一存儲結構的相關知識,希望對大家有所幫助2023-11-11使用C#實現(xiàn)RTP數(shù)據(jù)包傳輸 參照RFC3550
本篇文章小編為大家介紹,使用C#實現(xiàn)RTP數(shù)據(jù)包傳輸 參照RFC3550,需要的朋友參考下2013-04-04C#?基于TCP?實現(xiàn)掃描指定ip端口的方式示例
本文主要介紹了C#基于TCP實現(xiàn)掃描指定ip端口的方式示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11