ContextMenuMenuItemで使用しようとしているObservableCollectionがあります。
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());
}
しかし、最初の行はnullです。私はその方法論を数回使用しますが、ここでは失敗します。実際、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としてContextMenuに割り当てるにはどうすればよいですか?
編集:
//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");
}
//...
}
}