2013-07-03 25 views
8

我還沒有找到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中的設計時間中向下滾動。運行項目工作正常,用戶可以向下滾動到其他對象。

但在設計時間似乎沒有機會向下滾動到精確定位(現在隱藏的)控件。

一個解決方案是擴展控件以顯示完整的內容。但這不可能是最好的解決方案。有沒有人有設計時間正確滾動的線索?

非常感謝。

回答

12

不要認爲這存在一個開箱即用的設計時間屬性。但是你可以很容易地創建一個你自己。

這樣說:

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"> 
... 

在設計時單獨你應該能夠查看您的設計像

enter image description here

而在實際運行時控制將不被觸摸。在設計時,CustomDesignAttributes也具有用於水平偏移的類似性質local:CustomDesignAttributes.HorizontalScrollTo

+0

謝謝您的詳細和工作的回答,薇薇描述。我設法讓它工作。不管怎樣,微軟爲什麼放棄這種支持令人困惑。如果我沒有記錯的話,使用滾動面板的Windows窗體應用程序不支持在設計模式下滾動。 –

+0

我想知道是否有可能在Silverlight中做同樣的事情?我在Silverlight項目中嘗試了你的代碼,它拋出了'當前上下文中不存在'DependencyPropertyDescriptor'這個名字的錯誤。有什麼解決方法嗎?我發現這篇文章https://bkiener.wordpress.com/2010/08/27/listening-to-dependencyproperty-changes-in-silverlight/涉及到這個話題,但無法弄清楚如何將它應用於你的代碼。 –

2

還有另一種解決不滾動ScrollViewer問題的方法。基本上將ScrollViewer的內容製作成UserControl。然後你會編輯你的實際內容,就像編輯你的UserControl(單獨的文件和全部寬度)一樣。

它在更多的細節在此博客文章http://electricbeach.org/?p=862

+0

是否有Visual Studio的詳細信息?我被卡在步驟4 – prouser135

+0

@ prouser135好,我不是100%確定。由於VS中的GUI編輯器與Blend有點不同。但你可能仍然很幸運...... –

相關問題