2014-10-07 75 views
2

我目前正试图在我的应用程序中“跳转到”菜单。我希望它看起来和工作的方式就像在谷歌设计书here顶部的一个,在那里你在菜单中有该页面的每个部分的字幕(准确地说,那个导航部分具有诸如内容,Baseline Grids,Keylines and Spacing等)。如果可能的话,我希望它能够在不将其分割为新的MVVM视图的情况下工作,全部在一个UserControl内。是否有可能创建一些关键字,只需使用HTML中的“goto”链接制作菜单?跳转到UserControl内导航

<a href="#metrics-and-keylines-baseline-grids">Baseline Grids</a> 

也许是一个自定义的ScrollViewer?但是,如何知道UI中每个元素的高度?

回答

2

每个框架元素都有一个BringIntoView方法。你可以定义一个超链接或按钮菜单,无论它能够调用一个命令。该命令将采取的元素作为参数,并且命令执行将调用BringIntoView();

一个简单的例子:

public class JumpToElementCommand : ICommand 
{ 
    public void Execute(object parameter) 
    { 
     FrameworkElement frameworkElement = parameter as FrameworkElement; 

     if (frameworkElement != null) 
     { 
      frameworkElement.BringIntoView(); 
     } 
    } 

    public bool CanExecute(object parameter) 
    { 
     return parameter is FrameworkElement; 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 
} 

XAML用法:

<Window.Resources> 
<commands:JumpToElementCommand x:Key="JumpToElementCommand" /> 
</Window.Resources> 

<Button Command="{StaticResource JumpToElementCommand}" CommandParameter="{Binding ElementName=FirstJump}" /> 

<Grid x:Name="FirstJump"> 
</Grid> 

编辑:新增的CommandManager .RequerySuggested,以便在加载后重新评估命令参数。

+0

你有什么想法,我怎么能强制视图显示我想在顶部而不是'ScrollViewer'底部的项目? – mikes 2014-10-10 14:02:47

+1

@ mikes:看看这个:http://stackoverflow.com/questions/2946954/make-listview-scrollintoview-scroll-the-item-into-the-center-of-the-listview-c,你也可能是能够在最后一个导航点上调用BringIntoView,然后在导航点上调用BringIntoView,只要导航的导航点位于最后一个导航点的上方。 – 2014-10-10 14:54:17

0

现在(在Michael G的大力帮助下)我有答案。 首先为菜单制作一个ihnerited面板(对于我来说,它是父母ItemsControlStackPanel主人)。比订阅ItemsControl中的按钮项目的所有Click事件。作为CommandParameter绑定到一个元素要跳转到:

<Button CommandParameter="{Binding ElementName=jumpTarget}"/> 

而且在您使用Click事件处理程序的解决方案呈现here其中

Control_GotFocus(object sender, RoutedEventArgs e) 

istead您通过CommandParameter喜欢这里:

Control_GotFocus(FrameworkElement targetControl) 

你还必须得到父母ScrollViewer并把它作为AssociatedObject在上面的例子中( here解释了如何获取所选类型的父项)。当然,您可以通过简单地使用带有参数ItemsControl的每个Button添加参数的命令来避免事件subciption。