private void Prepare(ListBox target) { // The ListBox needs to be part of a rooted visual tree. var root = new TestRoot(); root.Child = target; // Apply the template to the ListBox itself. target.ApplyTemplate(); // Then to its inner ScrollViewer. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single(); scrollViewer.ApplyTemplate(); // Then make the ScrollViewer create its child. ((ContentPresenter)scrollViewer.Presenter).UpdateChild(); // Now the ItemsPresenter should be reigstered, so apply its template. target.Presenter.ApplyTemplate(); // Because ListBox items are virtualized we need to do a layout to make them appear. target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); // Now set and apply the item templates. foreach (ListBoxItem item in target.Presenter.Panel.Children) { item.Template = ListBoxItemTemplate(); item.ApplyTemplate(); item.Presenter.ApplyTemplate(); ((ContentPresenter)item.Presenter).UpdateChild(); } // The items were created before the template was applied, so now we need to go back // and re-arrange everything. foreach (IControl i in target.GetSelfAndVisualDescendants()) { i.InvalidateMeasure(); } target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); }
/// <summary> /// Shows the list of suggestions. If the current word is not misspelled /// this method does nothing. /// </summary> public void ShowSuggestions() { if (AreSuggestionsVisible || !IsCurrentWordMisspelled) { return; } // If this method was called by external code, // the list of suggestions will not be populated yet. if (suggestionList.ItemsSource == null) { AttemptToShowSuggestions(); return; } AdornerLayer layer = AdornerLayer.GetAdornerLayer(this); if (layer == null) { return; } // Position the adorner beneath the misspelled word. int idx = FindBeginningOfCurrentWord(); Rect rect = GetRectFromCharacterIndex(idx); adorner.SetOffsets(rect.Left, rect.Bottom); // Add the adorner into the adorner layer. layer.Add(adorner); // Since the ListBox might have a new set of items but has not // rendered yet, we force it to calculate its metrics so that // the height animation has a sensible target value. suggestionList.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); suggestionList.Arrange(new Rect(new Point(), suggestionList.DesiredSize)); // Animate the ListBox's height to the natural value. var anim = new DoubleAnimation { From = 0.0, To = suggestionList.ActualHeight, Duration = new Duration(TimeSpan.FromMilliseconds(200)), FillBehavior = FillBehavior.Stop }; suggestionList.BeginAnimation(HeightProperty, anim); areSuggestionsVisible = true; }
private void Prepare(ListBox target) { // The ListBox needs to be part of a rooted visual tree. var root = new TestRoot(); root.Child = target; // Apply the template to the ListBox itself. target.ApplyTemplate(); // Then to its inner ScrollViewer. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single(); scrollViewer.ApplyTemplate(); // Then make the ScrollViewer create its child. ((ContentPresenter)scrollViewer.Presenter).UpdateChild(); // Now the ItemsPresenter should be reigstered, so apply its template. target.Presenter.ApplyTemplate(); // Because ListBox items are virtualized we need to do a layout to make them appear. target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); // Now set and apply the item templates. foreach (ListBoxItem item in target.Presenter.Panel.Children) { item.Template = ListBoxItemTemplate(); item.ApplyTemplate(); item.Presenter.ApplyTemplate(); ((ContentPresenter)item.Presenter).UpdateChild(); } // The items were created before the template was applied, so now we need to go back // and re-arrange everything. foreach (IControl i in target.GetSelfAndVisualDescendents()) { i.InvalidateMeasure(); } target.Arrange(new Rect(0, 0, 100, 100)); }