0

このサンプルを使用して Windows Phone アプリを開発していました: Local Database Sample

そのサンプルでは、​​アイコンを使用して削除タスクが実装されています。による削除タスクを変更しましたContext Menu。しかし、それは私にはうまくいきません。

を押しDeleteても何も起こりません。

どんな間違いをしたかわかりません。

私の変更されたコード:

XAML コード:

<TextBlock 
    Text="{Binding ItemName}" 
    FontWeight="Thin" FontSize="28" 
    Grid.Column="0" Grid.Row="0"
    VerticalAlignment="Top">

    <toolkit:ContextMenuService.ContextMenu>
    <toolkit:ContextMenu Name="ContextMenu">
    <toolkit:MenuItem Name="Delete"  Header="Delete" Click="deleteTaskButton_Click"/>
    </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>

</TextBlock>

C# コード:

private void deleteTaskButton_Click(object sender, RoutedEventArgs e)
    {
        // Cast the parameter as a button.
        var button = sender as TextBlock;

        if (button != null)
        {
            // Get a handle for the to-do item bound to the button.
            ToDoItem toDoForDelete = button.DataContext as ToDoItem;
            App.ViewModel.DeleteToDoItem(toDoForDelete);
            MessageBox.Show("Deleted Successfully");
        }

        // Put the focus back to the main page.
        this.Focus();
    }

そのサンプルの元のコードの作業:

XAML コード:

<TextBlock 
    Text="{Binding ItemName}" 
    FontSize="{StaticResource PhoneFontSizeLarge}" 
    Grid.Column="1" Grid.ColumnSpan="2" 
    VerticalAlignment="Top" Margin="-36, 12, 0, 0"/>
<Button                                
    Grid.Column="3"
    x:Name="deleteTaskButton"
    BorderThickness="0"
    Margin="0, -18, 0, 0"
    Click="deleteTaskButton_Click">
<Image 
    Source="/Images/appbar.delete.rest.png"
    Height="75"
    Width="75"/>
</Button>

C# コード:

private void deleteTaskButton_Click(object sender, RoutedEventArgs e)
    {
        // Cast the parameter as a button.
        var button = sender as Button;

        if (button != null)
        {
            // Get a handle for the to-do item bound to the button.
            ToDoItem toDoForDelete = button.DataContext as ToDoItem;

            App.ViewModel.DeleteToDoItem(toDoForDelete);

            MessageBox.Show("Deleted Successfully");
        }

        // Put the focus back to the main page.
        this.Focus();
    }
4

1 に答える 1

0

あなたの場合senderは ではないTextBlockので、この行:

var button = sender as TextBlock;

戻り値null

にキャストできますMenuItem

using Microsoft.Phone.Controls;
....

private void deleteTaskButton_Click(object sender, RoutedEventArgs e)
{

    var item = sender as MenuItem;

    if (item!= null)
    {
        // Get a handle for the to-do item bound to the button.
        ToDoItem toDoForDelete = item .DataContext as ToDoItem;
        App.ViewModel.DeleteToDoItem(toDoForDelete);
        MessageBox.Show("Deleted Successfully");
    }

    // Put the focus back to the main page.
    this.Focus();
}
于 2013-09-11T08:11:41.883 に答える