1

我有我想要一個文本菜單菜單項使用一個ObservableCollection:如何將DataContext附加到此ContextMenu?

private void MenuItem_TapCopyDetails(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    //entries is a table in my datamodel 
    var currentItem = ((sender as MenuItem).DataContext) as entries; //when it doesn't work, this is null 
    if (currentItem.name == null) //and this throws a null reference exception 
     MessageBox.Show("null"); 
    else 
     MessageBox.Show(currentItem.name.ToString()); 
} 

但第一行是空的。我多次使用這種方法,但在這裏失敗了。實際上,您可以在XAML中看到另一個調用此方法的工具,它工作正常。

唯一的區別在於XAML;我使用略有不同的結合的TextBlocks:

<!--ContentPanel - place additional content here--> 
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
    <controls:Pivot Name="pivotControl" Margin="0,-20,0,0" SelectionChanged="Pivot_SelectionChanged"> 
     <controls:PivotItem Header="search" > 
      <StackPanel> 
       <ListBox Height="450" HorizontalAlignment="Left" Margin="0,10,0,0" 
         Name="listBoxHome" VerticalAlignment="Top" Width="434" 
         ItemsSource="{Binding Path=SearchPeopleResults}"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Tap="StackPanel_Tap" Margin="5,0,0,0"> 

           <!--These MenuItems work properly--> 
           <toolkit:ContextMenuService.ContextMenu> 
            <toolkit:ContextMenu> 
             <toolkit:MenuItem Header="copy details" Tap="MenuItem_TapCopyDetails" /> 
             <toolkit:MenuItem Header="bookmark person" Tap="MenuItem_Tap_BookmarkPerson" /> 
            </toolkit:ContextMenu> 
           </toolkit:ContextMenuService.ContextMenu> 

           <TextBlock Text="{Binding name}" TextTrimming="WordEllipsis" FontSize="22" /> 
           <TextBlock Text="{Binding dob}" TextTrimming="WordEllipsis" Margin="5,0,0,0" /> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </StackPanel> 
     </controls:PivotItem> 

     <controls:PivotItem Header="detail"> 
      <StackPanel Margin="5"> 
       <StackPanel Orientation="Horizontal" Name="stackDetail"> 

        <!--This MenuItem returns a null exception--> 
        <toolkit:ContextMenuService.ContextMenu> 
         <toolkit:ContextMenu> 
          <toolkit:MenuItem Header="copy details" Tap="MenuItem_TapCopyDetails" /> 
         </toolkit:ContextMenu> 
        </toolkit:ContextMenuService.ContextMenu> 

        <TextBlock Text="{Binding EntryDetails[0].name}" FontSize="48" /> 
        <TextBlock Text="{Binding EntryDetails[0].dob}" HorizontalAlignment="Right" Margin="10,0,0,0" VerticalAlignment="Bottom" /> 
       </StackPanel> 
       <TextBlock Text="{Binding EntryDetails[0].info}" FontSize="26.667" /> 

       <ListBox Height="317" HorizontalAlignment="Left" Margin="0,10,0,0" 
         Name="listBoxExamples" VerticalAlignment="Top" Width="434" 
         ItemsSource="{Binding Path=NotableQuotes}"> 
        <TextBlock Text="Examples sentences" Margin="0,5,0,0"/> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Margin="5,0,0,0"> 

           <!--These MenuItems work properly--> 
           <toolkit:ContextMenuService.ContextMenu> 
            <toolkit:ContextMenu> 
             <toolkit:MenuItem Header="copy quote" Tap="MenuItem_TapCopyQuote" /> 
             <toolkit:MenuItem Header="bookmark quote" Tap="MenuItem_Tap_BookmarkQuote" /> 
            </toolkit:ContextMenu> 
           </toolkit:ContextMenuService.ContextMenu> 

           <TextBlock Text="{Binding quote}" FontSize="22" TextWrapping="Wrap" Margin="0,15,0,0" /> 
           <TextBlock Text="{Binding date}" TextWrapping="Wrap" Margin="10,0,0,0" /> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </StackPanel> 
     </controls:PivotItem> 
    </controls:Pivot> 
</Grid> 

我如何分配entries或我的ObservableCollection作爲在DataContext到文本菜單?

編輯:

//View Model 
namespace AppDatabase.ViewModel 
{ 
    public class AppViewModel : INotifyPropertyChanged 
    { 
     //LINQ to SQL datacontext for the database. 
     private TheDataContext appDB; 

     public AppViewModel(string dbConnectionString) 
     { 
      appDB = new TheDataContext(dbConnectionString); 
     } 

     private ObservableCollection<entries> _searchPeopleResults; 
     public ObservableCollection<entries> SearchPeopleResults 
     { 
      get { return _searchPeopleResults; } 
      set { _searchPeopleResults = value; } 
     } 

     private ObservableCollection<entries> _entryDetals; 
     public ObservableCollection<entries> EntryDetails 
     { 
      get { return _entryDetals; } 
      set { _entryDetals = value; } 
     } 

     private ObservableCollection<quotes> _notableQuotes; 
     public ObservableCollection<quotes> NotableQuotes 
     { 
      get { return _notableQuotes; } 
      set { _notableQuotes = value; } 
     } 

     //... 

     //Query the database and load collections used by the pivot pages 
     public void LoadEntryDetails(int entryID) 
     { 
      var details = from entries entry in appDB.Entries 
          where entry.id.Equals(entryID) 
          select entry; 

      _entryDetals = new ObservableCollection<entries>(details); 
      NotifyPropertyChanged("EntryDetails"); 
     } 

     //... 
    } 
} 
+1

很難回答你的問題,而不瞭解支持你的XAML的模型。什麼是綁定到你的用戶界面? – ColinE

+1

@ColinE我已經添加了視圖模型,我不確定這是什麼意思。 – Louis

回答

0

比較與原第三行。這一個工程。

<controls:PivotItem Header="detail"> 
    <StackPanel Margin="5"> 
     <StackPanel Orientation="Horizontal" Name="stackDetail" DataContext="{Binding Path=EntryDetails[0]}"> 
相關問題