c# - Wpf 数据网格滚动条卡住

标签 c# wpf datagrid scrollbar

我是 WPF 新手。我有一个大约有 10000 行的数据网格。实现搜索和高亮功能,实现如下代码

<Style x:Key="DefaultCell" TargetType="{x:Type DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                        <ControlTemplate TargetType="DataGridCell">

                        <local:CustomTextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Content.Text}">
                            <!--InlineCollection="{Binding ., Converter={StaticResource StringToXamlConverter} }"/>-->
                            <local:CustomTextBlock.InlineCollection>
                              <MultiBinding Converter="{StaticResource StringToXamlConverter}">
                                    <Binding RelativeSource="{RelativeSource Self}" Path="." />
                                    <Binding RelativeSource="{RelativeSource Self}" Path="(local:SearchOperations.SearchTerm)"/>
                                </MultiBinding>
                            </local:CustomTextBlock.InlineCollection>                                
                        </local:CustomTextBlock>                     
                    </ControlTemplate>
                </Setter.Value> 
            </Setter>   

搜索和突出显示非常有效。但是在单击垂直滚动条时整个网格卡住。这可能是什么原因?

最佳答案

您可以在 Binding 上使用 IsAsync 属性。

<Binding RelativeSource="{RelativeSource Self}" Path="." IsAsync="True"/>

这将强制您的绑定(bind)发生在不同的线程上,从而使您的 UI 免于卡住。但是,由于您有很多行,这可能需要一段时间,因此我建议您也使用 FallbackValue

<Binding RelativeSource="{RelativeSource Self}" Path="." IsAsync="True" FallbackValue="..."/>

这将在异步过程发生时提供一个值,典型的值是文本,例如“正在加载...”消息。

关于c# - Wpf 数据网格滚动条卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749717/

相关文章:

c# - 解决不可变日期时间以在循环中添加一天

c# - 在不知道 T 的情况下保留 List<T> 的引用?

c# - 如何在 XAML 中显示递归属性

c# - 如何在运行时动态更改 DataGrid String 列格式?

wpf - 如何在 CellTemplate 中为 DataTemplate 设置特定的 Datacontext

c# - 为什么 IList<>.Reverse() 不像 List<>().Reverse 那样工作

c# - 如何在 Visual Studio 中将当前时间转换为毫秒?

c# - 如何从另一个 wpf 窗口更改 WPF 窗口标签文本

c# - MenuItem 背景不透明度而不是其文本?

sql-server - 使用 WPF 数据网格更新 SQL 数据库表