【问题标题】:Wpf datagrid scrollbar freezesWpf datagrid滚动条冻结
【发布时间】:2015-06-10 07:23:36
【问题描述】:

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

搜索和突出显示就像一个魅力。但是单击垂直滚动条时,整个网格会冻结。这可能是什么原因?

【问题讨论】:

  • 我的猜测是它正在为每一行运行 Converter。你能花一点时间来测试一下这个理论吗?
  • @MikeEason 是的,转换器必须遍历每个单元格才能实现搜索功能。
  • 解决方案参考Enable Virtualization

标签: c# wpf datagrid scrollbar


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 感谢您的解决方案,但不幸的是它不起作用。当我单击网格中的任何位置时,我观察到网格进入无响应状态。但是当加载网格时,在鼠标滚轮上移动行正在滚动。
猜你喜欢
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2012-11-17
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多