1

我创建了一个堆栈面板并将其添加到列表框中。堆栈面板包含两个文本块,第一个文本块文本是 id,其他文本是名称。来自数据库。我想删除特定 id 的堆栈面板。这是代码:

public partial class MainPage : PhoneApplicationPage
    {
        StackPanel stackpanel2 = null;
        Border br = null;
        TextBox tb1 = null;
        TextBox tb2 = null;
        string str1 = null;
        TextBlock tblk1 = null;
        TextBlock tblk2 = null;
        ListBox lb = null;

        public MainPage()
        {
            InitializeComponent();           
            createListBox();

        }
        public void createListBox()
        {
            updateDatabase();
            deleteRow(i);  
            lb = new ListBox();
            lb.Margin = new Thickness();
            lb.Height = double.NaN;
            lb.Width = double.NaN;            
            ContentPanel.Children.Add(lb);
            int rowCount = noofrows();
            for (int i = 1; i <= rowCount; i++)
            {

                createStackPanel(i);


                createTextBlock1(i);
                createTextBlock2(i);

                lb.Items.Add(br);
            }               

        }
        public void createTextBlock1(int y )
        {
            tblk1 = new TextBlock();
            tblk1.Height = 80;
            tblk1.Width = 150;
            tblk1.FontSize = 30;
            tblk1.Foreground = new SolidColorBrush(Colors.White);
           // tblk1.Margin = new Thickness();
            tblk1.Text = returnID(y);          
            stackpanel2.Children.Add(tblk1);
        }
        public void createTextBlock2(int a)
        {
            tblk2 = new TextBlock();
            tblk2.Height = 80;
            tblk2.Width = 150;
            tblk2.FontSize = 30;
            tblk2.Foreground = new SolidColorBrush(Colors.White);
            //tblk2.Margin = new Thickness();
            tblk2.Text = SelectName(a);
            stackpanel2.Children.Add(tblk2);
        }
        private void deleteRow(int x)
        {
            deleteStackPanel(x);
            string str2 = "delete  from Details where id =" + x;
            (Application.Current as App).db.SelectList(str2);

        }
        private void updateDatabase()
        {
            string str1 = "insert into Details (id,name,age,contact) values('1','sanjay','20','4444')";
            (Application.Current as App).db.SelectList(str1);
        }

        void createStackPanel(int c)
        {
            br = new Border();
            br.BorderBrush = new SolidColorBrush(Colors.Green);
            br.BorderThickness = new Thickness(5);
            stackpanel2 = new StackPanel();
            stackpanel2.Height = 100;
            stackpanel2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            stackpanel2.Margin = new Thickness();
            stackpanel2.Orientation = System.Windows.Controls.Orientation.Horizontal;
            stackpanel2.Background = new SolidColorBrush(Colors.Blue);
            br.Child = stackpanel2;
            stackpanel2.Tap += (s, e) =>
                {
                    RowValue.rowcount = c ;
                    NavigationService.Navigate(new Uri("/Details.xaml", UriKind.Relative));
                };

        }
        public String SelectName(int x)
        {

            return Convert.ToString((Application.Current as App).db.SelectList("select name from details where id ="+x));
        }
        public string returnID(int z)
        {
            return Convert.ToString((Application.Current as App).db.SelectList("select id from details where id ="+z));
        }
        public Int32 noofrows()
        {
            int b = Convert.ToInt32((Application.Current as App).db.SelectList("select count(*) from details"));
            return b;
        }
        public void  deleteStackPanel(int x)
        {
            lb.Items.Remove(stackpanel2);         
        }

    }

    public static class RowValue
    {
        public static int rowcount = 0;
    }
4

1 回答 1

1

让我稍微简化一下您的代码。

转到 xaml 并编写以下内容:

   <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox ItemsSource="{Binding Items}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderThickness="5" Background="Green">
                    <StackPanel Height="100" HorizontalAlignment="Left"
                                Orientation="Horizontal"
                                Background="Blue"   >
                            <TextBlock Height="80" Width="150" 
                                       FontSize="30" Foreground="White" 
                                       Text="{Binding Id}"/>
                            <TextBlock Height="80" Width="150" 
                                       FontSize="30" Foreground="White" 
                                       Text="{Binding Name}"/>
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

我们声明的布局与您在代码中所做的相同。

然后去MainPage.xaml.cs写以下内容:

public partial class MainPage : PhoneApplicationPage
{
public class DataBaseEntry 
{
    public string Id {get;set;}
    public string Name {get;set;}
}

public ObservableCollection<DataBaseEntry> Items {get;set;}

public MainPage()
{
    InitializeComponent();
    Items = new ObservableCollection<DataBaseEntry>();
    PopulateListBox();

}

public void PopulateListBox()
{
    updateDatabase();
    deleteRow(i);

    int rowCount = noofrows();
    for (int i = 1; i <= rowCount; i++)
    {
        var entry = new DataBaseEntry
        {
            Id = returnID(i),
            Name = SelectName(i)
        }
        Items.Add(entry);
    }

}

//this will remove item from Items collection and updates listbox
public void DeleteItemById(int id) 
{
    var item = Items.FirstOrDefault(item => item.id == id.ToString());
    if (item != null) 
    {
        Items.Remove(item);
    }
    string str2 = "delete  from Details where id =" + id;
    (Application.Current as App).db.SelectList(str2);
}

我建议您阅读有关Xaml的更多信息——它是一种创建和管理 UI 的优雅方式。

如果您需要有关如何处理列表框中选定项目的更多信息,请阅读此博客文章。

于 2012-09-12T08:05:32.843 回答