WPF 数据网格滚动条冻结
本文关键字:滚动条 冻结 网格 数据网 数据 WPF | 更新日期: 2023-09-27 17:55:11
我是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"/>
这将强制绑定在不同的线程上进行,从而使 UI 免于冻结。但是,由于您有很多行,这可能需要一段时间,所以我建议也使用FallbackValue
。
<Binding RelativeSource="{RelativeSource Self}" Path="." IsAsync="True" FallbackValue="..."/>
这将在异步过程发生时提供一个值,典型的值是文本,例如"正在加载..."消息。