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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Unity UGUI的InputField輸入框組件使用詳解
這篇文章主要為大家介紹了Unity UGUI的InputField輸入框組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
WPF實(shí)現(xiàn)動(dòng)畫效果(四)之緩動(dòng)函數(shù)
這篇文章介紹了WPF實(shí)現(xiàn)動(dòng)畫效果之緩動(dòng)函數(shù),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹
這篇文章主要介紹了C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹,vshost.exe是一個(gè)宿主進(jìn)程,主要用來提高調(diào)試效率,需要的朋友可以參考下2015-01-01
C#利用System.Threading.Thread.Sleep即時(shí)輸出信息的詳解
本篇文章是對(duì)C#利用System.Threading.Thread.Sleep即時(shí)輸出信息進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
詳解C#如何利用爬蟲技術(shù)實(shí)現(xiàn)快捷租房
做為一個(gè)碼農(nóng),大部分都集中在一二線城市,所以租房也就無可避免,面對(duì)如今五花八門的租房信息,往往很難找到合適的房子。本文教你如何利用爬蟲技術(shù)實(shí)現(xiàn)快捷租房,感興趣的可以了解一下2022-09-09

