我尚未找到有关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中的设计时间中向下滚动。运行Project效果很好,用户可以向下滚动到其他对象。

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

一种解决方案是扩展控件以显示完整的内容。但这不是最好的解决方案。有谁知道在设计时进行适当滚动的线索吗?

非常感谢你。

最佳答案

不要以为这没有现成的设计时属性。但是,您可以轻松地自己创建一个。

像这样说:

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

关于wpf - 如何在Blend中的“设计时”中滚动ScrollViewer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17442094/

10-09 18:06
查看更多