不要認爲這存在一個開箱即用的設計時間屬性。但是你可以很容易地創建一個你自己。
這樣說:
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
。
來源
2013-07-03 11:33:20
Viv
謝謝您的詳細和工作的回答,薇薇描述。我設法讓它工作。不管怎樣,微軟爲什麼放棄這種支持令人困惑。如果我沒有記錯的話,使用滾動面板的Windows窗體應用程序不支持在設計模式下滾動。 –
我想知道是否有可能在Silverlight中做同樣的事情?我在Silverlight項目中嘗試了你的代碼,它拋出了'當前上下文中不存在'DependencyPropertyDescriptor'這個名字的錯誤。有什麼解決方法嗎?我發現這篇文章https://bkiener.wordpress.com/2010/08/27/listening-to-dependencyproperty-changes-in-silverlight/涉及到這個話題,但無法弄清楚如何將它應用於你的代碼。 –