如何将DataContext附加到此ContextMenu?

时间:2012-04-30 03:57:35

标签: c# windows-phone-7 contextmenu observablecollection datacontext

我有一个ObservableCollection,我试图使用ContextMenu MenuItem:

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指定为ContextMenu的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 个答案:

答案 0 :(得分:0)

将第三行与原始行进行比较。这个有效。

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