开发者

Silverlight: Disable UI virtualization?

开发者 https://www.devze.com 2023-04-11 01:56 出处:网络
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

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:LayoutO开发者_如何学编程verrides="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);


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消