从xaml的列表框中选择listitem
问题描述:
我有一个列表框,列表框中的数据正在通过sqlite数据库填充。从xaml的列表框中选择listitem
XAML
<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我可以这样做吗? 当我点击删除按钮时,列表没有被选中,但删除图标被选中。
答
因为没有关于列表视图中包含的数据的数据类型的信息。我已经写下了你的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);
}
}
}
什么是你的'deleteTaskButton_Click()'方法模样的名称替换
YourDataType
与您的数据类型和secondListBoxobj
? – mcalex