Tôi đang cố tạo một điều khiển Graph bằng cách sử dụng một ListBox WPF. Tôi tạo ra Canvas của riêng mình có nguồn gốc từ một VirtualizingPanel và tôi xử lý việc thực hiện và ảo hóa của bản thân mình.WPF Listbox Virtualization tạo ra DisconnectedItems
Bảng điều khiển mặt hàng của hộp danh sách sau đó được đặt làm canvas ảo được tùy chỉnh của tôi.
Vấn đề tôi đang gặp phải xảy ra trong các tình huống sau:
- ListBox Mục A được tạo ra đầu tiên.
- Mục Hộp danh sách B được tạo ở bên phải của Mặt hàng A trên canvas.
- Mục Hộp danh sách A được ảo hóa trước tiên (bằng cách xoay nó ra khỏi chế độ xem).
- Mục Hộp danh mục B được ảo hóa thứ hai (một lần nữa bằng cách kéo nó ra khỏi chế độ xem).
- Mang ListBox mục A và B theo quan điểm (ví dụ: hiện thực hóa chúng)
- Sử dụng Snoop, tôi phát hiện rằng ListBox có tại 3 hạng mục, một trong số họ trở thành một "DisconnectedItem" nằm ngay bên dưới ListBox mục B.
Điều gì tạo ra "DisconnectedItem" này? Nếu tôi đã ảo hóa B trước, tiếp theo là A, mục này sẽ không được tạo. Lý thuyết của tôi là các mục ảo hóa trước các mục khác trong ListBox khiến trẻ em bị ngắt kết nối.
Vấn đề thậm chí còn rõ ràng hơn khi sử dụng biểu đồ có hàng trăm nút, khi tôi kết thúc với hàng trăm mục bị ngắt khi tôi xoay quanh.
Đây là một phần của mã cho vải:
/// <summary>
/// Arranges and virtualizes child element positionned explicitly.
/// </summary>
public class VirtualizingCanvas : VirtualizingPanel
{
(...)
protected override Size MeasureOverride(Size constraint)
{
ItemsControl itemsOwner = ItemsControl.GetItemsOwner(this);
// For some reason you have to "touch" the children collection in
// order for the ItemContainerGenerator to initialize properly.
var necessaryChidrenTouch = Children;
IItemContainerGenerator generator = ItemContainerGenerator;
IDisposable generationAction = null;
int index = 0;
Rect visibilityRect = new Rect(
-HorizontalOffset/ZoomFactor,
-VerticalOffset/ZoomFactor,
ActualWidth/ZoomFactor,
ActualHeight/ZoomFactor);
// Loop thru the list of items and generate their container
// if they are included in the current visible view.
foreach (object item in itemsOwner.Items)
{
var virtualizedItem = item as IVirtualizingCanvasItem;
if (virtualizedItem == null ||
visibilityRect.IntersectsWith(GetBounds(virtualizedItem)))
{
if (generationAction == null)
{
GeneratorPosition startPosition =
generator.GeneratorPositionFromIndex(index);
generationAction = generator.StartAt(startPosition,
GeneratorDirection.Forward, true);
}
GenerateItem(index);
}
else
{
GeneratorPosition itemPosition =
generator.GeneratorPositionFromIndex(index);
if (itemPosition.Index != -1 && itemPosition.Offset == 0)
{
RemoveInternalChildRange(index, 1);
generator.Remove(itemPosition, 1);
}
// The generator needs to be "reseted" when we skip some items
// in the sequence...
if (generationAction != null)
{
generationAction.Dispose();
generationAction = null;
}
}
++index;
}
if (generationAction != null)
{
generationAction.Dispose();
}
return default(Size);
}
(...)
private void GenerateItem(int index)
{
bool newlyRealized;
var element =
ItemContainerGenerator.GenerateNext(out newlyRealized) as UIElement;
if (newlyRealized)
{
if (index >= InternalChildren.Count)
{
AddInternalChild(element);
}
else
{
InsertInternalChild(index, element);
}
ItemContainerGenerator.PrepareItemContainer(element);
element.RenderTransform = _scaleTransform;
}
element.Measure(new Size(double.PositiveInfinity,
double.PositiveInfinity));
}
Bạn có đang tái chế bình chứa không? – Paparazzi
@Blam: Tôi không nghĩ tôi là gì, bạn có ý gì khi tái chế hộp chứa? –
Chỉ cần tìm kiếm msdn cho thùng chứa rác http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizationmode.aspx Chỉ cần tiếp cận mà còn chỉ là một nhận xét – Paparazzi