不要认为为此存在开箱即用的设计时属性。但是,您可以很容易地自己创建一个。
像这样说:
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
public static class CustomDesignAttributes {
private static bool? _isInDesignMode;
public static DependencyProperty VerticalScrollToProperty = DependencyProperty.RegisterAttached(
"VerticalScrollTo",
typeof(double),
typeof(CustomDesignAttributes),
new PropertyMetadata(ScrollToChanged));
public static DependencyProperty HorizontalScrollToProperty = DependencyProperty.RegisterAttached(
"HorizontalScrollTo",
typeof(double),
typeof(CustomDesignAttributes),
new PropertyMetadata(ScrollToChanged));
private static bool IsInDesignMode {
get {
if (!_isInDesignMode.HasValue) {
var prop = DesignerProperties.IsInDesignModeProperty;
_isInDesignMode =
(bool)DependencyPropertyDescriptor.FromProperty(prop, typeof(FrameworkElement)).Metadata.DefaultValue;
}
return _isInDesignMode.Value;
}
}
public static void SetVerticalScrollTo(UIElement element, double value) {
element.SetValue(VerticalScrollToProperty, value);
}
public static double GetVerticalScrollTo(UIElement element) {
return (double)element.GetValue(VerticalScrollToProperty);
}
public static void SetHorizontalScrollTo(UIElement element, double value) {
element.SetValue(HorizontalScrollToProperty, value);
}
public static double GetHorizontalTo(UIElement element) {
return (double)element.GetValue(HorizontalScrollToProperty);
}
private static void ScrollToChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
if (!IsInDesignMode)
return;
ScrollViewer viewer = d as ScrollViewer;
if (viewer == null)
return;
if (e.Property == VerticalScrollToProperty) {
viewer.ScrollToVerticalOffset((double)e.NewValue);
} else if (e.Property == HorizontalScrollToProperty) {
viewer.ScrollToHorizontalOffset((double)e.NewValue);
}
}
}
现在通过在您的 xaml 中设置自定义附加属性,例如:
<ScrollViewer Height="200"
local:CustomDesignAttributes.VerticalScrollTo="50">
...
仅在设计时,您应该能够使用滚动偏移量查看您的设计,例如
而在实际运行时,控件将不会被触及。CustomDesignAttributes
设计时水平偏移也有类似的属性local:CustomDesignAttributes.HorizontalScrollTo
。