WPF拖動(dòng)DataGrid滾動(dòng)條時(shí)內(nèi)容混亂的解決方法
在WPF中,如果DataGrid里使用了模板列,當(dāng)拖動(dòng)滾動(dòng)條時(shí),往往會(huì)出現(xiàn)列表內(nèi)容顯示混亂的情況。解決方法就是在Binding的時(shí)候給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>
后臺(tái)代碼如下:
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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解C#中Dictionary<TKey,TValue>的存儲(chǔ)結(jié)構(gòu)
無論是實(shí)際的項(xiàng)目中,還是在我們學(xué)習(xí)的過程中,都會(huì)重點(diǎn)的應(yīng)用到Dictionary<TKey,?TValue>這個(gè)存儲(chǔ)類型,所以本文就來為大家介紹一下這一存儲(chǔ)結(jié)構(gòu)的相關(guān)知識(shí),希望對(duì)大家有所幫助2023-11-11使用Spire.Barcode程序庫生成二維碼的實(shí)例解析
這篇文章主要介紹了使用Spire.Barcode程序庫生成二維碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12使用C#實(shí)現(xiàn)RTP數(shù)據(jù)包傳輸 參照RFC3550
本篇文章小編為大家介紹,使用C#實(shí)現(xiàn)RTP數(shù)據(jù)包傳輸 參照RFC3550,需要的朋友參考下2013-04-04C#?基于TCP?實(shí)現(xiàn)掃描指定ip端口的方式示例
本文主要介紹了C#基于TCP實(shí)現(xiàn)掃描指定ip端口的方式示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11