2014-10-11 105 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

相關問題