Ben senin sorunu yeniden veremeyiz (benim için ListBox
yeni koleksiyonunda son öğeye kaydırılan ItemsSource
değiştirirken). Her neyse, ListBox
'u en iyi şekilde kaydırmak için ItemsSource
her değiştiğinde arkasında bir kod kullanabilirsiniz. İlk ItemsSourceProperty
değişikliklere dinlemek ve onun ürün
oluşturulduktan sonra sonra üstüne ListBox
kaydırmak Güncelleme
yerine bu arkasındaki kodu önlemek için yaptığı ekli bir davranış yaptı.
Basit bir çözümdür: Bu
<ListBox ...
behaviors:ScrollToTopBehavior.ScrollToTop="True"/>
ScrollToTopBehavior
public static class ScrollToTopBehavior
{
public static readonly DependencyProperty ScrollToTopProperty =
DependencyProperty.RegisterAttached
(
"ScrollToTop",
typeof(bool),
typeof(ScrollToTopBehavior),
new UIPropertyMetadata(false, OnScrollToTopPropertyChanged)
);
public static bool GetScrollToTop(DependencyObject obj)
{
return (bool)obj.GetValue(ScrollToTopProperty);
}
public static void SetScrollToTop(DependencyObject obj, bool value)
{
obj.SetValue(ScrollToTopProperty, value);
}
private static void OnScrollToTopPropertyChanged(DependencyObject dpo,
DependencyPropertyChangedEventArgs e)
{
ItemsControl itemsControl = dpo as ItemsControl;
if (itemsControl != null)
{
DependencyPropertyDescriptor dependencyPropertyDescriptor =
DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, typeof(ItemsControl));
if (dependencyPropertyDescriptor != null)
{
if ((bool)e.NewValue == true)
{
dependencyPropertyDescriptor.AddValueChanged(itemsControl, ItemsSourceChanged);
}
else
{
dependencyPropertyDescriptor.RemoveValueChanged(itemsControl, ItemsSourceChanged);
}
}
}
}
static void ItemsSourceChanged(object sender, EventArgs e)
{
ItemsControl itemsControl = sender as ItemsControl;
EventHandler eventHandler = null;
eventHandler = new EventHandler(delegate
{
if (itemsControl.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
{
ScrollViewer scrollViewer = GetVisualChild<ScrollViewer>(itemsControl) as ScrollViewer;
scrollViewer.ScrollToTop();
itemsControl.ItemContainerGenerator.StatusChanged -= eventHandler;
}
});
itemsControl.ItemContainerGenerator.StatusChanged += eventHandler;
}
}
Ve GetVisualChild bir uygulamaya
private T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
Sorunuzu doğru anladınız mı yoksa başka bir şey mi arıyorsunuz? –
@Meleak Teşekkürler! Bu yaklaşım yararlı olacaktır. Ben aslında benim liste kutusu benim http://blogs.msdn.com/b/dancre/archive/2006/02/16/implementing-a-virtualizingpanel-part adresinden aldığım bir VirtualizingTilePanel sorununu izledim -4-goods.aspx. Sorun, dize aşağı kaydırarak örnek yüklemede çoğaltılabilir ve sonra da ilişkili koleksiyonu tek bir öğe içeren bir taneyle değiştirebilir. Fare tekerleği ile yukarı gidinceye kadar görünüm boş kalır. – devdigital
VirtualizingTilePanel için http://social.msdn.microsoft.com/Forums/en/wpf/thread/05c5868b-7c91-4726-81fb-9af634c74d4a adresinde bir yanıt buldu, ancak sanallaştırıcı paketlemeye ikili dağıtımdan geçtim - http://www.binarymission.co.uk/binaryvirtwrappanelsl3.htm – devdigital