Silverlight:禁用 UI 虚拟化?

发布于 2024-12-09 02:42:53 字数 1204 浏览 0 评论 0原文

有没有一种简单的方法可以禁用 ListBox 控件上的 UI 虚拟化? 我尝试使用“FindName()”方法在 ListBox 控件中查找控件,但如果该控件明显位于 Web 浏览器窗口之外,则无法找到该控件。我几乎可以肯定罪魁祸首是 UI 虚拟化。 当控件滚动离开页面时,它不再通过“FindName()”成功检索。

当我将其滚动回到屏幕上时,它成功返回控制。

这是这个问题的扩展:

Silverlight: FrameworkElement.FindName() 在浏览器窗口中不“可见”时找不到控件

使用编码示例更新

这是我尝试检索控件的背后代码。 “DynamicTagFormFields”是ListBox 控件。

textField tf = DynamicTagFormFields.FindName(s.KeyValue) as textField;

如果最终用户可以在屏幕上查看我尝试检索的实际 textField 控件,则这将返回有效的“textField”对象。 但是,如果我使用 ListBox 的垂直滚动条将 textField 控件滚动到视图之外,然后再次强制该过程,则上述代码将返回 null。

这是 ListBox 的 XAML:

 <ListBox x:Name="DynamicTagFormFields" Margin="0" Style="{StaticResource ListBoxStyle1}" ItemContainerStyle="{StaticResource ListBoxItemStyle4}" d:LayoutOverrides="Height" Grid.Row="2" IsTabStop="False" TabNavigation="Local" ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>

使用以下代码以编程方式将 textField 对象动态添加到 ListBox:

DynamicTagFormFields.Items.Add(textFieldControl);

Is there a simple way to disable UI virtualization on a ListBox control?
I'm attempting to find a control within a ListBox control using the "FindName()" method, but in the case that the control is visibly off of the Web Browser window, it's not finding the control. I'm almost certain the culprit is UI virtualization.
As the control is scrolled off the page, it is no longer retrieved successfully via "FindName()".

The second I scroll it back onto the screen, it returns the control successfully.

This is an extension of this question:

Silverlight: FrameworkElement.FindName() not finding the control when it's not "visible" in the browser window

Update with coding example

This is the code behind where I attempt to retrieve the control.
"DynamicTagFormFields" is the ListBox control.

textField tf = DynamicTagFormFields.FindName(s.KeyValue) as textField;

This returns a valid "textField" object if the actual textField control that I'm attempting to retrieve is viewable on the screen to the end user.
However, if I scroll the textField control out of view using the ListBox's vertical scroll bar, then force the process again, the aforementioned code will return null.

This is the XAML of the ListBox:

 <ListBox x:Name="DynamicTagFormFields" Margin="0" Style="{StaticResource ListBoxStyle1}" ItemContainerStyle="{StaticResource ListBoxItemStyle4}" d:LayoutOverrides="Height" Grid.Row="2" IsTabStop="False" TabNavigation="Local" ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>

The textField object is dynamically added to the ListBox programmatically with the following code:

DynamicTagFormFields.Items.Add(textFieldControl);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青芜 2024-12-16 02:42:53

您是否尝试过此操作:-

<ListBox x:Name="DynamicTagFormFields" Margin="0" Style="{StaticResource ListBoxStyle1}"
    ItemContainerStyle="{StaticResource ListBoxItemStyle4}" d:LayoutOverrides="Height"
    Grid.Row="2" IsTabStop="False" TabNavigation="Local"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
       <ItemsPanelTemplate>
           <StackPanel />
       </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

默认面板是 VirtualisingStackPanel,这可能是导致您的问题的原因。

Have you tried this:-

<ListBox x:Name="DynamicTagFormFields" Margin="0" Style="{StaticResource ListBoxStyle1}"
    ItemContainerStyle="{StaticResource ListBoxItemStyle4}" d:LayoutOverrides="Height"
    Grid.Row="2" IsTabStop="False" TabNavigation="Local"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
       <ItemsPanelTemplate>
           <StackPanel />
       </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

The default panel is the VirtualisingStackPanel which may be the cause of your problem.

杀お生予夺 2024-12-16 02:42:53


http://blogs.msdn.com/b/mcsuksoldev/archive/2010/04/13/performance-characteristics-of-the-silverlight-datagrid.aspx
它说了以下有关 DataGrid 虚拟化的信息,显示了两种关闭它的方法,第一种是在 DataGrid 周围添加 ScrollViewer,实际上并不建议将其作为关闭行虚拟化的方法,因为 DataGrid 有标题行,因此它们还展示了如何更改其 XAML 模板。对于 ListBox,虽然没有这样的标头,但将其包装在 ScrollViewer 中可能是一个可行的选择,给它无限的大小,从而关闭行虚拟化

...在 DataGrid 周围放置一个 ScrollViewer。这给出了数据网格
无限大小并有效关闭虚拟化。很遗憾
在我的项目中,我不小心这样做了,但没有意识到效果
关于性能。您确实需要使用 DataGrid 的滚动
条形图,而不是 ScrollViewer。请注意,如果您想关闭 UI
虚拟化(例如对于小型网格)您可以重新模板化 DataGrid
并将 RowsPresenter 放入 ScrollViewer 中,这将再次导致
它认为它有无限大。这很有用,因为你不会
当您滚动时,不断获取 LoadingRow 和 UnloadingRow 事件。
请小心操作,以便列标题正确滚动
(请参阅本文末尾附录中的 XAML)。

at
http://blogs.msdn.com/b/mcsuksoldev/archive/2010/04/13/performance-characteristics-of-the-silverlight-datagrid.aspx
it says the following informative stuff regarding the DataGrid's virtualization, showing two ways that you can switch it off, the first one being to add a ScrollViewer around the DataGrid which is not in fact suggested as a method to turn off row virtualization, since DataGrid has a header row, so they also show how to change its XAML template. For ListBox though that doesn't have such header, it could be a viable option to wrap it in a ScrollViewer, giving it infinite size and thus turning off the row virtualization

...drop a ScrollViewer round your DataGrid. This gives the DataGrid
infinite size and effectively turns off virtualization. Unfortunately
on my project I’d done this accidentally without realising the effect
on performance. You really need to be using the DataGrid’s scroll
bars, not a ScrollViewer. Note that if you want to turn off UI
Virtualization (eg for small grids) you can re-template the DataGrid
and put the RowsPresenter inside a ScrollViewer which again will cause
it to think it has infinite size. This is useful because you won’t
keep getting LoadingRow and UnloadingRow events as you scroll around.
Be careful to do it right so that the column headers scroll correctly
(see XAML in the appendix at the end of this article).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文