2017-08-01 69 views
2

我有一個列表框,列表框中的數據正在通過sqlite數據庫填充。從xaml的列表框中選擇listitem

XAML

enter image description here

<ListBox x:Name="listBoxobj" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1" SelectionChanged="listBoxobj_SelectionChanged"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid Width="330" Height="100" > 
       <Border Margin="5" BorderBrush="White" BorderThickness="1"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="50" /> 
          <ColumnDefinition Width="*" /> 
          <ColumnDefinition Width="100" /> 
         </Grid.ColumnDefinitions> 
         <CheckBox Name="Option1CheckBox" Grid.Row="0" Grid.Column="0" IsChecked="{Binding _isComplete}" VerticalAlignment="Center" Margin="5,0,0,0" /> 
         <TextBlock x:Name="NameTxt" Grid.Row="0" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="22" Foreground="White"/> 
         <TextBlock x:Name="Age" Grid.Row="1" Grid.Column="1" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" FontSize="22" Text="{Binding Age}" /> 
         <Button Grid.Row="0" Grid.Column="2" x:Name="deleteTaskButton" BorderThickness="0" Margin="0" Click="deleteTaskButton_Click" Height="18"> 
          <Image Source="/Assets/delete.png"/> 
         </Button> 
        </Grid> 
       </Border> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

當deleteTaskButton用戶點擊它存在於每個列表項中,ListBoxItem的數據應該從列表中刪除,並移動到另一個出現在接下來的樞軸表page.How我可以這樣做嗎? 當我點擊刪除按鈕時,列表沒有被選中,但刪除圖標被選中。

+0

什麼是你的'deleteTaskButton_Click()'方法模樣的名稱替換YourDataType與您的數據類型和secondListBoxobj? – mcalex

回答

0

因爲沒有關於列表視圖中包含的數據的數據類型的信息。我已經寫下了你的deleteTaskButton_Click方法應該看起來如何。與第二listbox

private void deleteTaskButton_Click(object sender, RoutedEventArgs e) 
    { 
     var dataContext = (sender as Button).DataContext as YourDataType; 
     if (dataContext != null) 
     { 
      //now you have the item that was clicked to delete. 
      var DeleteFromDelete = listBoxobj.ItemsSource as ICollection<YourDataType>; 
      if (DeleteFromDelete != null) 
      { 
       //this removes the item to be removed from the currently viewing listview. 
       DeleteFromDelete.Remove(dataContext); 

       //now add the item that was deleted into the other listview. 
       var TobeAddedIntoList = secondListBoxobj.ItemsSource as ICollection<YourDataType>; 
       if (TobeAddedIntoList != null) 
        TobeAddedIntoList.Add(dataContext); 
      } 
     } 
    }