【问题标题】:Select listitem from listbox in xaml从 xaml 的列表框中选择列表项
【发布时间】:2017-08-01 13:32:45
【问题描述】:

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

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 时,列表框项数据应该从列表中删除并移动到下一个 Pivot 页面中的另一个列表。我该怎么做? 当我点击删除按钮时,列表没有被选中,但删除图标被选中。

【问题讨论】:

  • 你的deleteTaskButton_Click() 方法是什么样的?

标签: c# listbox windows-phone-8.1


【解决方案1】:

因此没有关于列表视图中包含的数据的数据类型的信息。我已经写下了您的 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);
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多