1

我有一个正在尝试与 ContextMenu MenuItem 一起使用的 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());
}

但是第一行是空的。我多次使用该方法,但在这里失败了。实际上,正如您在 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");
        }

        //...
    }
}
4

1 回答 1

0

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

<controls:PivotItem Header="detail">
    <StackPanel Margin="5">
        <StackPanel Orientation="Horizontal" Name="stackDetail" DataContext="{Binding Path=EntryDetails[0]}">
于 2012-05-02T07:57:03.183 回答