wpf的虛擬化技術會使UI的控件只初始化看的到的子元素, 而不是所有子元素都被初始化,這樣會提高UI性能。
但是我們經常會遇到一個問題:
應用虛擬化后看不見的子元素因為沒有實際產生導致ItemContainerGenerator的查找元素方法(ContainerFromIndex /?ContainerFromItem)失效。
解決辦法1:
(1)監聽ItemsControl的ItemContainerGenerator的StatusChanged事件, 當GeneratorStatus為ContainerGenerated時再進行查找,
(2)遍歷ItemsControl的Items,獲取當前需要查找元素的Index,
(3)利用反射調用VirtualizingPanel的BringIndexIntoView,或者直接調用BringIntoView,然后強制滾動以產生Item(具體可以參考TreeViewItem的ExpandRecursicve的內部實現)。
需要注意的是:
(1)ItemContainerGenerator的StatuChanged事件會多次被觸發, 原因是每次初始化的Item數量就是當前空間所能看到的數量,StatusChanged觸發的次數就是總共Items除以每次初始的Item數量。
(2)調用BringIndexInToView不正確會導致InvalidOperationException,具體為“Cannot call StartAt when content generation is in progress.” ?或者 ”無法在正在進行內容生成時調用StartAt。”。 可以用Dispatcher.BeginInvoke來解決, 如下面代碼。
(3)當然ItemsControl中的虛擬化模式設置為Recycling, 即?VirtualizingStackPanel.VirtualizationMode?="Recycling"時,后端存儲的子元素選中項會在ItermContainerGenerator重新產生子項時變為DisconnectedItem。可以把模式設置為Standard解決。
具體代碼如下:
- 1. 查找入口?
???????private?ItemsControl?_currentSelectedItem?=?null;
???????private?void?BtnFind_Click(?object?sender?,?System.?Windows.RoutedEventArgs?e)
?????? {
???????????if?(string?.IsNullOrEmpty(?txtContent.Text?))
?????????? {
???????????????return;
?????????? }
???????????if?(_currentSelectedItem?==?null)
?????????? {
???????????????_currentSelectedItem?=?_treeView?;
?????????? }
???????????else
?????????? {
???????????????if?(_currentSelectedItem?is?TreeViewItem)
?????????????? {
?????????????????? (?_currentSelectedItem?as?TreeViewItem).?IsExpanded?=?true?;
?????????????? }
?????????? }
???????????if?(_currentSelectedItem?.ItemContainerGenerator.?Status?!=?GeneratorStatus?.ContainersGenerated)
?????????? {
???????????????_currentSelectedItem.ItemContainerGenerator?.StatusChanged?-=?new?EventHandler(ItemContainerGenerator_StatusChanged?);
???????????????_currentSelectedItem.ItemContainerGenerator?.StatusChanged?+=?new?EventHandler(ItemContainerGenerator_StatusChanged?);
?????????? }
???????????else
?????????? {
???????????????treeViewItem_BringIntoView(txtContent?.Text);
?????????? }
?????? }
- 2.StatusChanged事件的處理
????void?ItemContainerGenerator_StatusChanged?(object?sender,?EventArgs?e)
?????? {
???????????var?generator?=?sender?as?ItemContainerGenerator?;
???????????if?(null?==?generator)
?????????? {
???????????????return;
?????????? }
???????????//once the children have been generated, expand those children's children then remove the event handler
???????????if?(generator?.Status?==?GeneratorStatus.ContainersGenerated?&&?_currentSelectedItem?.ItemContainerGenerator.?Status?==?GeneratorStatus?.ContainersGenerated)
?????????? {
???????????????treeViewItem_BringIntoView(txtContent?.Text);
?????????? }
?????? }
- 3.具體虛擬化時的強制產生子元素及查找處理
private?void?treeViewItem_BringIntoView(string?findItem)
?????? {
?????????? System.Diagnostics.?Debug.WriteLine("enter treeViewItem_BringIntoview"?);
???????????try
?????????? {
?????????????? _currentSelectedItem.ApplyTemplate();
???????????????ItemsPresenter?itemsPresenter = (ItemsPresenter)_currentSelectedItem.Template.FindName("ItemsHost", (FrameworkElement)_currentSelectedItem);
???????????????if?(itemsPresenter !=?null?)
?????????????????? itemsPresenter.ApplyTemplate();
???????????????else
?????????????????? _currentSelectedItem.UpdateLayout();
???????????????VirtualizingPanel?virtualizingPanel = _currentSelectedItem.GetItemsHost()?as?VirtualizingPanel;
?????????????? virtualizingPanel.CallEnsureGenerator();
???????????????int?selectedIndex = -1;
???????????????int?count1 = _currentSelectedItem.Items.Count;
???????????????for?(int?i = 0; i < count1; i++)
?????????????? {
???????????????????ItemsItem1?tviItem = _currentSelectedItem.Items.GetItemAt(i)?as?ItemsItem1;
???????????????????if?(null?!= tviItem && tviItem.Label.Equals(findItem))
?????????????????? {
?????????????????????? selectedIndex = i;
???????????????????????break;
?????????????????? }
?????????????? }
???????????????if?(selectedIndex < 0)
?????????????? {
???????????????????return;
?????????????? }
???????????????Action?action = () =>
?????????????? {
???????????????????TreeViewItem?itemSelected =?null?;
???????????????????//Force to generate every treeView item by using scroll item
???????????????????if?(virtualizingPanel !=?null?)
?????????????????? {
???????????????????????try
?????????????????????? {
?????????????????????????? virtualizingPanel.CallBringIndexIntoView(selectedIndex);
?????????????????????? }
???????????????????????catch?(System.Exception?ex)
?????????????????????? {
?????????????????????????? System.Diagnostics.?Debug.WriteLine("CallBringIndexIntoView exception : "?+ ex.Message);
?????????????????????? }
?????????????????????? itemSelected = (TreeViewItem)_currentSelectedItem.ItemContainerGenerator.ContainerFromIndex(selectedIndex);
?????????????????? }
???????????????????else
?????????????????? {
?????????????????????? itemSelected = (TreeViewItem)_currentSelectedItem.ItemContainerGenerator.ContainerFromIndex(selectedIndex);
?????????????????????? itemSelected.BringIntoView();
?????????????????? }
???????????????????if?(null?!= itemSelected)
?????????????????? {
?????????????????????? _currentSelectedItem = itemSelected;
?????????????????????? (_currentSelectedItem?as?TreeViewItem?).IsSelected =?true;
?????????????????????? _currentSelectedItem.BringIntoView();
?????????????????? }
?????????????? };
?????????????? Dispatcher.BeginInvoke(?DispatcherPriority.Background, action);
?????????? }
???????????catch?(System.Exception?ex)
?????????? {
???????????????//
?????????? }
?????? }
- 4.xaml代碼
<Window
???xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
???xmlns:x?="http://schemas.microsoft.com/winfx/2006/xaml"
???xmlns:d?="http://schemas.microsoft.com/expression/blend/2008"
???xmlns:mc?="http://schemas.openxmlformats.org/markup-compatibility/2006"
???mc:Ignorable?="d"
???x:Class?="WpfApplication1.MainWindow"
???x:Name?="Window"
???Title="MainWindow"
???Width="640"
???Height="480" >
???<Window.Resources>
???????<HierarchicalDataTemplate
???????????x:Key?="ItemsItem1Template"
???????????ItemsSource="{Binding?Items}" >
???????????<StackPanel>
???????????????<TextBlock
???????????????????Text="{Binding?Label}" />
???????????</StackPanel>
???????</HierarchicalDataTemplate>
???</Window.Resources>
???<Grid
???????x:Name?="LayoutRoot" >
???????<TreeView
???????????x:Name?="_treeView"
???????????HorizontalAlignment="Left"
???????????Width="287"
???????????d:DataContext?="{Binding}"
???????????ItemsSource="{Binding?Items,?Source?={StaticResource?SampleDataSource?}}"
???????????ItemTemplate="{DynamicResource?ItemsItem1Template}"
???????????VirtualizingStackPanel.IsVirtualizing?="True"
? ? ? ? ? ?VirtualizingStackPanel.VirtualizationMode?="Standard"
? ? ? ? ? ? />
???????<Button
???????????x:Name?="btnFind"
???????????Content="Find"
???????????HorizontalAlignment="Right"
???????????Margin="0,8,8,0"
???????????VerticalAlignment="Top"
???????????Width="75"
???????????Click="BtnFind_Click" />
???????<TextBox
???????????x:Name?="txtContent"
???????????Margin="291,8,87,0"
???????????TextWrapping="Wrap"
???????????VerticalAlignment="Top"
???????????Height="21.837" />
???</Grid>
</Window>
- 5.反射系統控件私有方法的類
public?static?class?WPFUIElementExtension
?? {
?????? #region?Functions to get internal members using reflection
???????// Some functionality we need is hidden in internal members, so we use reflection to get them
?????? #region?ItemsControl.ItemsHost
???????static?readonly?PropertyInfo?ItemsHostPropertyInfo?=?typeof?(ItemsControl).?GetProperty("ItemsHost"?,?BindingFlags.Instance?|?BindingFlags.?NonPublic);
???????public?static?Panel?GetItemsHost(this?ItemsControl?itemsControl)
?????? {
???????????Debug.Assert?(itemsControl?!=?null);
???????????return?ItemsHostPropertyInfo?.GetValue(?itemsControl,?null?)?as?Panel;
?????? }
?????? #endregion?ItemsControl.ItemsHost
?????? #region?Panel.EnsureGenerator
???????private?static?readonly?MethodInfo?EnsureGeneratorMethodInfo?=?typeof(Panel?).GetMethod(?"EnsureGenerator",?BindingFlags?.Instance?|?BindingFlags.NonPublic?);
???????public?static?void?CallEnsureGenerator(this?Panel?panel)
?????? {
???????????Debug.Assert?(panel?!=?null);
???????????EnsureGeneratorMethodInfo.Invoke?(panel,?null);
?????? }
?????? #endregion?Panel.EnsureGenerator
?????? #region?VirtualizingPanel.?BringIndexIntoView
???????private?static?readonly?MethodInfo?BringIndexIntoViewMethodInfo?=?typeof(VirtualizingPanel?).GetMethod(?"BringIndexIntoView",?BindingFlags?.Instance?|?BindingFlags.NonPublic?);
???????public?static?void?CallBringIndexIntoView(this?VirtualizingPanel?virtualizingPanel,?int?index)
?????? {
???????????Debug.Assert?(virtualizingPanel?!=?null);
???????????BringIndexIntoViewMethodInfo.Invoke?(virtualizingPanel,?new?object?[] {?index?});
?????? }
?????? #endregion?VirtualizingPanel.?BringIndexIntoView
?????? #endregion?Functions to get internal members using reflection
?? }
解決方法2:
(1)參考方法1的第一步解決方法
(2)遍歷ItemsControl的Items, 根據ContainerFromIndex去找到當前可見的元素的index。
(3)利用BringInoView去滾動現有的Item以便UI產生后續的子元素, 然后循環直到找見要查找的子元素。(遍歷分為2部分,向前遍歷和向后遍歷)
注意事項:
(1)參考方法1的第一注意事項
(2)因為比方法1多了一次循環遍歷,當items很多時有點卡頓,不過還在可以忍受的范圍。
具體代碼:
1.參考方法1的代碼,具體只有強制生成子元素的方法有區別, 即與方法1中的步驟3有區別。
2.如下:
?private?void?treeViewItem_BringIntoView2(string?findItem)
?????? {
?????????? System.Diagnostics.?Debug?.WriteLine("enter treeViewItem_BringIntoview"?);
???????????try
?????????? {
?????????????? _currentSelectedItem.ApplyTemplate();
???????????????ItemsPresenter?itemsPresenter = (ItemsPresenter?)_currentSelectedItem.Template.FindName(?"ItemsHost", (FrameworkElement?)_currentSelectedItem);
???????????????if?(itemsPresenter !=?null?)
?????????????????? itemsPresenter.ApplyTemplate();
???????????????else
?????????????????? _currentSelectedItem.UpdateLayout();
???????????????VirtualizingPanel?virtualizingPanel = _currentSelectedItem.GetItemsHost()?as?VirtualizingPanel?;
?????????????? virtualizingPanel.CallEnsureGenerator();
???????????????TreeViewItem?itemTemp =?null?;
???????????????ItemsItem1?objTemp =?null?;
???????????????int?visiableIndex = -1;
???????????????int?findIndex = -1;
???????????????int?count1 = _currentSelectedItem.Items.Count;
???????????????for?(int?i = 0; i < count1; i++)
?????????????? {
?????????????????? itemTemp = (?TreeViewItem?)_currentSelectedItem.ItemContainerGenerator.ContainerFromIndex(i);
???????????????????if?(null?!= itemTemp)
?????????????????? {
?????????????????????? visiableIndex = i;
?????????????????? }
?????????????????? objTemp = _currentSelectedItem.Items.GetItemAt(i)?as?ItemsItem1?;
???????????????????if?(null?!= objTemp && objTemp.Label.Equals(findItem))
?????????????????? {
?????????????????????? findIndex = i;
?????????????????? }
?????????????? }
???????????????if?(findIndex == -1 || visiableIndex == -1)
?????????????? {
???????????????????return?;
?????????????? }
???????????????if?(findIndex < visiableIndex)
?????????????? {
???????????????????for?(int?j = visiableIndex; j >= findIndex; j--)
?????????????????? {
?????????????????????? itemTemp = (TreeViewItem?)_currentSelectedItem.ItemContainerGenerator.ContainerFromIndex(j);
???????????????????????if?(null?!= itemTemp)
?????????????????????? {
?????????????????????????? itemTemp.BringIntoView();
?????????????????????? }
?????????????????? }
?????????????? }
???????????????else?if?(findIndex > visiableIndex)
?????????????? {
???????????????????for?(int?j = visiableIndex; j <= findIndex; j++)
?????????????????? {
?????????????????????? itemTemp = (TreeViewItem?)_currentSelectedItem.ItemContainerGenerator.ContainerFromIndex(j);
???????????????????????if?(null?!= itemTemp)
?????????????????????? {
?????????????????????????? itemTemp.BringIntoView();
?????????????????????? }
?????????????????? }
?????????????? }
???????????????else
?????????????? {
?????????????????? itemTemp = (TreeViewItem?)_currentSelectedItem.ItemContainerGenerator.ContainerFromIndex(visiableIndex);
???????????????????if?(null?!= itemTemp)
?????????????????? {
?????????????????????? itemTemp.BringIntoView();
?????????????????? }
?????????????? }
???????????????if?(null?!= itemTemp)
?????????????? {
?????????????????? _currentSelectedItem = itemTemp;
?????????????????? (_currentSelectedItem?as?TreeViewItem?).IsSelected =?true;
?????????????????? _currentSelectedItem.BringIntoView();
?????????????? }
?????????? }
???????????catch?(System.Exception?ex)
?????????? {
???????????????//
?????????? }
?????? }
- 前言
- win32與WPF的混合編程
- WPF: 一個可以用StoryBoard動態改變Grid行寬/列高的類
- MFC中調用WPF教程
- Expression Blend操作: 使用behavior來控制Storyboard
- WPF DatePicker 的textbox的焦點
- WPF 使用MultiBinding ,TwoWay ,ValidationRule ,需要注意的事項
- WPF TreeView 后臺C#選中指定的Item, 需要遍歷
- WPF GridViewColumn Sort DataTemplate
- DataGridColum的bug
- WPF Get Multibinding Expression, Update Source,
- WPF 后臺觸發 Validate UI‘s Element
- WPF ValidationRule 觸發ErrorTemplate 的注意事項
- WPF DelegateCommand CanExecute
- WPF TextBox PreviewTextInput handle IME (chinese)
- No overload for &#39;OnStartup&#39; matches delegate &#39;System.Windows.StartupEventHandler&#39;
- WPF error: does not contain a static &#39;Main&#39; method suitable for an entry point
- WPF GridView中的CellTemplate失效的原因
- DataGrid 顯示選中的item
- 如何得到WPF中控件綁定的EventTrigger
- 選中DataGrid的Cell而不是row
- ContextMenu的自定義
- 輸入框只能輸入英文
- TextBox的OnTextboxChanged事件里對Text重新賦值帶中文, 導致崩潰
- DataGrid當列寬超出當前寬度時,沒有數據也恒有滾動條
- wpf如何獲取control template里的元素
- Set connectionId threw an exception.
- WPF中Visible設為Collapse時,VisualTreeHelper.GetChildrenCount為0
- XAML 編碼規范 (思考)
- 如何為現有控件的DependencyProperty添加Value Changed事件?
- TreeView滾動TreeViewItem
- 為BindingList添加Sort
- WPF Background的設置有坑
- 自定義Panel中添加依賴屬性需要注意的問題
- TextBlock截斷字符顯示為....
- DataGrid 支持字符截斷顯示
- TreeView控件實踐
- WPF如何更改系統控件的默認高亮顏色 (Highlight brush)
- ViewModel中C# Property自動添加OnPropertyChanged處理的小工具, 以及相應Python知識點
- WPF中Xaml編譯正常而Designer Time時出錯的解決辦法
- 關于Snoop的用法
- wpf中為DataGrid添加checkbox支持多選全選
- WPF中DataGrid控件的過濾(Filter)性能分析及優化
- wpf控件提示Value ‘’ can not convert
- DropShadowEffect導致下拉框控件抖動
- 再論WPF中的UseLayoutRounding和SnapsToDevicePixels
- WPF案例:如何設計歷史記錄查看UI
- WPF案例:如何設計搜索框(自定義控件的原則和方法)
- WPF基本概念入門
- WPF開發中Designer和碼農之間的合作
- 聊聊WPF中的Dispatcher
- 聊聊WPF中字體的設置
- Bug:DataGridCell的顯示不完整
- WPF中ToolTip的自定義
- WPF中ItemsControl綁定到Google ProtocolBuffer的結構體時的性能問題
- TreeView的性能問題
- Xaml中string(字符串)常量的定義以及空格的處理
- 依賴屬性
- WPF中的CheckBox的_ (underscore / 下劃線)丟失
- WPF錯誤:必須使“Property”具有非 null 值。
- WPF中ItemsControl應用虛擬化時找到子元素的方法
- WPF毫秒級桌面時鐘的實現-C#中Hook(鉤子)的應用
- KB2464222導致IsNonIdempotentProperty方法找不見
- WPF中PreviewMouseDownEvent的系統處理:TabItem的PreviewMouseDown 事件彈框后不切換的問題調查
- WPF文字渲染相關的問題及解決
- wpf中的默認右鍵菜單中的復制、粘貼、剪貼等沒有本地化的解決方案
- WPF內部DeliverEvent讀鎖和PrivateAddListener寫鎖導致死鎖
- Windbg調試WPF的依賴屬性
- WPF 后臺Render線程崩潰, Exception from HRESULT: 0x88980406
- WPF中DependencyObject與DependencyProperty的源碼簡單剖析
- 禁用WPF中DataGrid默認的鼠標左鍵拖動多選行的效果
- wpf工程中在Xaml文件下添加多個cs文件
- ScrollViewer滾動到底來觸發加載數據的Behavior