从xaml中的列表框中选择listitem

时间:2017-08-01 13:32:46

标签: c# listbox windows-phone-8.1

我有一个列表框,列表框中的数据将通过sqlite数据库填充。

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>

当用户点击每个listitem中存在的deleteTaskButton时,listboxitem数据应该从列表中删除并移动到下一个Pivot页面中的另一个列表。我该怎么做? 当我单击删除按钮时,列表未被选中,但选择了删除图标。

1 个答案:

答案 0 :(得分:0)

因为没有关于listview中包含的数据的数据类型的信息。我已经写下了deleteTaskButton_Click方法的外观。将YourDataType替换为您的数据类型,将secondListBoxobj替换为第二个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);
            }
        }
    }