簡體   English   中英

為什么在更新DataGrid的ItemsSource時水平滾動條會重置其位置?

[英]Why does the horizontal scrollbar reset its position when I update the ItemsSource of the DataGrid?

我有一個帶有DataGrid的小型WPF應用程序。 當我像這樣更新我的dataSource

DataTable myTable = GetNewTableWithChangedRows();
MyGrid.ItemsSource = myTable.DefaultView;

垂直滾動條停留在用戶定義的位置,而水平滾動條將其位置重置為零。

區別在哪里? 以及如何使水平滾動條停留在用戶定義的位置?

發生這種情況的原因是, DataGrid在更改項目源時會重新生成所有列。 列寬將再次計算,因此滾動查看器將重置其水平位置。

垂直位置(和行選擇)將通過設計保持: Selector將嘗試保持當前選擇。

如果要保持水平滾動位置不變,則必須防止自動生成列。 這意味着,您必須手動定義DataGrid列:

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Header 1" Binding="{Binding Column1Value}"/>
        <DataGridTextColumn Header="Header 2" Binding="{Binding Column2Value}"/>
        <!-- ...and so on, for each column you need -->
    </DataGrid.Columns>
</DataGrid>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM