WPF拖動DataGrid滾動條時內(nèi)容混亂的解決方法
更新時間:2016年10月12日 10:31:44 作者:sunny906
這篇文章主要介紹了WPF拖動DataGrid滾動條時內(nèi)容混亂的解決方法
在WPF中,如果DataGrid里使用了模板列,當(dāng)拖動滾動條時,往往會出現(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Unity UGUI的InputField輸入框組件使用詳解
這篇文章主要為大家介紹了Unity UGUI的InputField輸入框組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹
這篇文章主要介紹了C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹,vshost.exe是一個宿主進(jìn)程,主要用來提高調(diào)試效率,需要的朋友可以參考下2015-01-01
C#利用System.Threading.Thread.Sleep即時輸出信息的詳解
本篇文章是對C#利用System.Threading.Thread.Sleep即時輸出信息進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

