2014-10-11 103 views
0

我想要找到哪个PivotItem当前正在显示,但总是我在CommandParameter中收到空值。下面的代码:Pivot SelectedItem属性

xaml

<Pivot ItemsSource="{Binding Categories}" x:Name="MainList"> 
    ... 
</Pivot> 

... 

<Page.BottomAppBar> 
    <CommandBar> 
     <AppBarButton Label="add item" Icon="Add" Command="{Binding AddElementCommand}" CommandParameter="{Binding ElementName=MainList, Path=SelectedItem}" />    
    </CommandBar> 
</Page.BottomAppBar> 

C#

public DelegateCommand<object> AddElementCommand { get; set; } 

public void Init() 
{ 
    this.AddElementCommand = new DelegateCommand<object>(this.AddElement); 
} 

private void AddElement(object category) 
{ 
    ... 
} 

我100%肯定的,那DelegateCommand实施工作,因为如果我把CommandParameter{Binding},那么我接收AddElement的当前ViewModel对象的参数。

为什么通过ElementName绑定不起作用?是否可以绑定枢轴的SelectedItem?

回答

0

是的,您可以简单地通过使用SelctionChanged事件得到Pivot控件中的当前PivotItem

这可以帮助你:

How to detect a Pivot item is selected in Windows Phone 7

+0

是的,但我想使用MVVM模式和不使用的行为,如果有可能。另外,我正在开发Windows Phone 8.1(XAML)。 – Fka 2014-11-13 14:12:30

相关问题