這是我的AutoToolItemToCenter TreeView與DataTrigger的解決方案,因此它不是滾動的選定項目。
在XAML我通過DataTrigger
添加自定義行爲
<Style TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelectedInGrid}" Value="true">
<Setter Property="local:TreeViewScrollBehavior.ScrollItemToCenter" Value="true" />
</DataTrigger>
</Style.Triggers>
我試圖從http://www.shujaat.net/2010/08/attached-behaviors.html
public static class TreeViewScrollBehavior
{
public static DependencyProperty ScrollItemToCenterProperty =
DependencyProperty.RegisterAttached("ScrollItemToCenter", typeof(bool),
typeof(TreeViewScrollBehavior), new UIPropertyMetadata(false, OnScrollItemToCenter));
public static bool GetScrollItemToCenter(DependencyObject obj)
{
return (bool)obj.GetValue(ScrollItemToCenterProperty);
}
public static void SetScrollItemToCenter(DependencyObject obj, bool value)
{
obj.SetValue(ScrollItemToCenterProperty, value);
}
public static void OnScrollItemToCenter(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
//TreeViewItem t = GetTreeViewItem(d);
TreeViewItem t = (TreeViewItem)d;
TryScrollToCenterOfView(GetTree(t), t);
}
}
這個簡單的例子之後創建自己的行爲,並添加上述的稍微改變功能鏈接
private static TreeView GetTree(TreeViewItem item)
{
var parent = VisualTreeHelper.GetParent(item);
while (!(parent is TreeView))
{
parent = VisualTreeHelper.GetParent(parent);
}
return parent as TreeView;
}
private static void TryScrollToCenterOfView(ItemsControl itemsControl, UIElement container)
{
// Find the container
//var container = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
if (container == null) return;
// Find the ScrollContentPresenter
ScrollContentPresenter presenter = null;
for (Visual vis = container; vis != null && vis != itemsControl; vis = VisualTreeHelper.GetParent(vis) as Visual)
if ((presenter = vis as ScrollContentPresenter) != null)
break;
if (presenter == null) return;
// Find the IScrollInfo
var scrollInfo =
!presenter.CanContentScroll ? presenter :
presenter.Content as IScrollInfo ??
FirstVisualChild(presenter.Content as ItemsPresenter) as IScrollInfo ??
presenter;
// Compute the center point of the container relative to the scrollInfo
Size size = container.RenderSize;
Point center = container.TransformToAncestor((Visual)scrollInfo).Transform(new Point(size.Width/2, size.Height/2));
center.Y += scrollInfo.VerticalOffset;
center.X += scrollInfo.HorizontalOffset;
// Adjust for logical scrolling
if (scrollInfo is StackPanel || scrollInfo is VirtualizingStackPanel)
{
double logicalCenter = itemsControl.ItemContainerGenerator.IndexFromContainer(container) + 0.5;
Orientation orientation = scrollInfo is StackPanel ? ((StackPanel)scrollInfo).Orientation : ((VirtualizingStackPanel)scrollInfo).Orientation;
if (orientation == Orientation.Horizontal)
center.X = logicalCenter;
else
center.Y = logicalCenter;
}
// Scroll the center of the container to the center of the viewport
if (scrollInfo.CanVerticallyScroll) scrollInfo.SetVerticalOffset(CenteringOffset(center.Y, scrollInfo.ViewportHeight, scrollInfo.ExtentHeight));
if (scrollInfo.CanHorizontallyScroll) scrollInfo.SetHorizontalOffset(CenteringOffset(center.X, scrollInfo.ViewportWidth, scrollInfo.ExtentWidth));
}
private static double CenteringOffset(double center, double viewport, double extent)
{
return Math.Min(extent - viewport, Math.Max(0, center - viewport/2));
}
private static DependencyObject FirstVisualChild(Visual visual)
{
if (visual == null) return null;
if (VisualTreeHelper.GetChildrenCount(visual) == 0) return null;
return VisualTreeHelper.GetChild(visual, 0);
}
這是我的完整解決方案在xaml.cs或DataObject中無需代碼滾動TreeView。
THX的行爲的idead用戶AwkwardCoder
如果任何人有一些改進的竅門,我想聽聽它;-)
您使用的是MVVM模式或做法背後一個純代碼? – AwkwardCoder 2014-09-25 11:26:09
我試試MVVM。但即時通訊這個 – 2014-09-25 11:34:39