如何在 Blend 的设计时滚动 ScrollViewer

Posted

技术标签:

【中文标题】如何在 Blend 的设计时滚动 ScrollViewer【英文标题】:How do I scroll a ScrollViewer in Design Time in Blend 【发布时间】:2013-07-03 07:28:06 【问题描述】:

我还没有找到有关 Blend / WPF 的此问题的信息。只为Eclipse 而这无济于事。

我目前正在设计一个 WPF 4 应用程序对话框。它应该是一个ScrollViewer,在StackPanel 中具有不同的元素:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Height="470" VerticalAlignment="Top">
  <StackPanel Height="1893" Width="899">
    <!-- Elements here ... -->
  </StackPanel>
<ScrollViewer>

到目前为止,一切都按预期工作。滚动条可见。我的问题是我无法在 Blend 或 Visual Studio 2012 中在设计时向下滚动。运行项目工作正常,用户可以向下滚动到其他对象。

但在设计时似乎没有机会向下滚动以准确定位(现在隐藏的)控件。

对此的一种解决方案是展开控件以显示完整的内容。但这不是最好的解决方案。有没有人知道设计时正确滚动的线索?

非常感谢。

【问题讨论】:

【参考方案1】:

不要认为为此存在开箱即用的设计时属性。但是,您可以很容易地自己创建一个。

这样说:

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 用于设计时的水平偏移。

【讨论】:

感谢您的详细而有效的回答,Viv。我已经设法让它工作了。尽管如此,微软放弃支持的原因令人困惑。如果我没记错的话,带有滚动面板的 Windows 窗体应用程序确实支持在设计模式下滚动。 我想知道在 Silverlight 中是否可以做同样的事情?我在 Silverlight 项目中尝试了您的代码,它抛出 The name 'DependencyPropertyDescriptor' does not exist in the current context 错误。有什么解决方法吗?我发现这篇文章bkiener.wordpress.com/2010/08/27/… 触及了该主题,但无法弄清楚如何将其应用于您的代码。【参考方案2】:

还有另一种方法可以解决非滚动 ScrollViewer 的问题。基本上把你的 ScrollViewer 的内容变成一个 UserControl。然后您将编辑您的实际内容,就像您编辑您的 UserControl(单独的文件和全宽)一样。

在这篇博客文章http://electricbeach.org/?p=862中有更详细的描述

【讨论】:

有 Visual Studio 的详细信息吗?我被困在第 4 步 @prouser135 好吧,我不是 100% 确定。因为 VS 中的 GUI 编辑器与 Blend 中的有点不同。但你可能仍然很幸运......

以上是关于如何在 Blend 的设计时滚动 ScrollViewer的主要内容,如果未能解决你的问题,请参考以下文章

Expression Blend 2 中 Silverlight 控件的设计时渲染

设计模式下的表达式 Blend 中的示例数据和运行应用程序时的实时数据

在另一个类的滚动视图中添加图像?

如何修改 WPF FlowDocumentScrollViewer 滚动条样式

Blend 4 中的 WP7 缺少设计视图

blend 2012 - 不要在后台运行整个应用程序