我是 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/